This is a bit messy to do in MySQL, given the lack of a preg_replace
– the following uses a nested REPLACE
to strip all numbers from the title:
function wpse167989_posts_orderby( $orderby, $query ) {
global $wpdb;
// Strip all numbers from title, and trim any leading spaces.
return $wpdb->prepare(
'LTRIM(' . str_repeat( 'REPLACE(', 10 ) . $wpdb->posts . '.post_title'
. str_repeat( ', %s, %s)', 10 ) . ') ' . $query->get( 'order' )
, 0, '', 1, '', 2, '', 3, '', 4, '', 5, '', 6, '', 7, '', 8, '', 9, '' );
}
Then
add_filter( 'posts_orderby', 'wpse167989_posts_orderby', 10, 2 );
$loop = new WP_Query( $args );
remove_filter( 'posts_orderby', 'wpse167989_posts_orderby', 10 );
EDIT per comments: to remove hyphens in addition, the filter becomes
function wpse167989_posts_orderby( $orderby, $query ) {
global $wpdb;
// Strip all numbers & hyphens from title, and trim any leading spaces.
return $wpdb->prepare(
'LTRIM(' . str_repeat( 'REPLACE(', 11 ) . $wpdb->posts . '.post_title'
. str_repeat( ', %s, %s)', 11 ) . ') ' . $query->get( 'order' )
, 0, '', 1, '', 2, '', 3, '', 4, '', 5, '', 6, '', 7, '', 8, '', 9, '', '-', '' );
}