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

  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.

  • Hi. Are you talking about having paginated child pages lists and when on page 2, 3 the order is not consistent? Or do you have a list of all child pages on a parent page and then when you display a child page you are also showing its child pages? – Vlad Olaru Feb 14 '18 at 8:33
  • You can check it out here: As you see, on the parent page, all child pages are listed alphabetically, but the article is ignored (as it should be). But once you are on a child page and navigate with the "previous" and "next" buttons, a different order is used. What I want is to use the same sorting order – with the article ignored – when I navigate through the various siblings. – Simifilm Feb 14 '18 at 10:19
  • 1
    Ok. Now it's easier to understand what you are after. Will help you shortly. Can you share the code you are using for generating the next and previous links? – Vlad Olaru Feb 15 '18 at 9:53
  • There is a interesting thread concerning the article issue on WordPress Development: »Ignoring initial articles (like 'a', 'an' or 'the') when sorting queries?«. It started from the same source you're using, but it is overall more detailed. – Nicolai Feb 18 '18 at 19:47

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 ) );
| improve this answer | |
  • Thank you. It took some tinkering, but it now works as I wanted. There's a quite important typo BTW, it must say 'posts_per_page'. – Simifilm Feb 18 '18 at 21:24
  • You are welcome. Oh you're right, next time you can just edit, I get notified and approve it. – Nicolai Feb 18 '18 at 22:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.