I'm attempting to sort my posts on a page while ignoring 'a', 'an' and 'the'. I found a good example on this page: https://css-tricks.com/ignoring-the-in-wordpress-queries/

My Query

$args = array(
    'post_type' => 'post',
    'posts_per_page' => -1

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( $args );

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 

My functions.php

function wpcf_create_temp_column($fields) {
  global $wpdb;
  $matches = 'The|A|An';
  $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;

The query is sorting posts that begin with 'the' and 'an' correctly. It's not sorting posts beginning with 'a' correctly though. For example, 'A League of their own', shows up right after 'Eagle'. 'A Short Film' is right after 'The Horror of Party Beach', 'A Chorus Line' is after 'Horton.' Any ideas what this could be?

  • This isn't the type of thing MySQL was built for, and would be much slower than a standard sort. It may be possible, but it would require custom SQL almost certainly, and be non-performant. Software such as Elastic Search would be the optimal and easiest option for this – Tom J Nowell Mar 30 '17 at 13:44
  • Thanks, I ended up putting all of my values in PHP arrays, sorting, then displaying. Worked well. Thanks – Tom Mar 30 '17 at 18:42

That's because you are matching from the fourth character as it was supposed to be used only with "the " (three characters onwards). Then the author of article added an "an " which has a similar effect. I think he didn't test for "a " after all.

Your code matches this:

Horror of Party Beach

hort Film


horus Line

See: THEN trim(substr($wpdb->posts.post_title from 4)

You should use a regex to trim the trailing A|AN|THE as well or even sort them using PHP.

| improve this answer | |
  • Do you know how I would trim the trailing A|AN|THE ? I tried figuring it out, with no luck. Thanks! – Tom Mar 30 '17 at 18:06
  • nevermind... I ended up going the PHP sort route. Thanks! – Tom Mar 30 '17 at 18:42

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.