Sort order of next/prev sibling page

I have a page with a couple of children pages. On the parent page I want all children page listed alphabetically by title, but I want the sorting to ignore articles like "The" or "A". I achieved this with code I got from here.

Here's my functions.php:

function wpcf_create_temp_column($fields) {
  global $wpdb;
  $matches = 'The';
  $has_the = " CASE 
      WHEN $wpdb-posts.post_title regexp( '^($matches)[[:space:]]' )
        THEN trim(substr($wpdb-posts.post_title from 4)) 
      ELSE $wpdb-posts.post_title 
        END AS title2";
  if ($has_the) {
    $fields .= ( preg_match( '/^(\s+)?,/', $has_the ) ) ? $has_the : ", $has_the";
  }
  return $fields;
}

function wpcf_sort_by_temp_column ($orderby) {
  $custom_orderby = " UPPER(title2) ASC";
  if ($custom_orderby) {
    $orderby = $custom_orderby;
  }
  return $orderby;
}

And here's my Wordpress query:

add_filter('posts_fields', 'wpcf_create_temp_column'); // Add the temporary column filter
add_filter('posts_orderby', 'wpcf_sort_by_temp_column'); // Add the custom order filter

$query = new WP_Query(array('post_type' = 'post')); // Your custom query

remove_filter('posts_fields','wpcf_create_temp_column'); // Remove the temporary column filter
remove_filter('posts_orderby', 'wpcf_sort_by_temp_column'); // Remove the temporary order filter 

if (have_posts()) : while ($query-have_posts()) : $query-the_post(); // The query output

  the_title(); 
  echo 'br/';

endwhile; endif; wp_reset_postdata();

This part works smoothly. My problem is that I want to have Next/Prev links on the respective sibling pages, and here the order of the pages should be identical to the one on the parent page. It doesn't really make sense IMO if I present the pages in a certain order on the parent page and then completely change the ordering once you access a child page.

I tried various ways to create Next/Prev links, but none of them allowed me to control the sort ordering of the pages the way I want.

Topic nextpage order child-pages Wordpress

Category Web


I think in this case it is probably easiest to create your own function or two. I'm giving you a rough outline on how to do it below.

// use the same filter to get the same results
add_filter( 'posts_fields', 'wpcf_create_temp_column' );
add_filter( 'posts_orderby', 'wpcf_sort_by_temp_column' );
// perform query
$q = new WP_Query( [ 
  'post_type' => 'pages', 
  'fields' => 'ids', 
  'posts_per_page' => -1 
] );
remove_filter( 'posts_fields','wpcf_create_temp_column' );
remove_filter( 'posts_orderby', 'wpcf_sort_by_temp_column' );

// array of ids
$a = $q->posts;

// index of current post
$i = array_search( get_the_ID(), $a );

// id of previous post
$pp_id = $a[ $i - 1 ];
// with the id get the permalink or whatever you need
$pp_pl = get_the_permalink( $pp_id );

// id of next post
$np_id = $a[ $i + 1 ];
// with the id get the title or whatever you need
$np_ti = esc_html( get_the_title( $np_id ) );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.