#StackBounty: #order #child-pages #nextpage Sort order of next/prev sibling page

Bounty: 100

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.


Get this bounty!!!

Leave a Reply