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?