Wordpress - Using Orderby and meta_value_num to order numbers first then strings
The OrderBy
argument can take more then one parameter so the solution was to change :
'orderby' => 'meta_value_num',
to:
'orderby' => 'meta_value meta_value_num',
I found this solution by combining code by @bonger and https://stackoverflow.com/questions/18084199/wordpress-query-order-by-case-when
And it works well.
Function
function filter_case($orderby = '') {
$orderby .= "CASE WHEN wp_postmeta.meta_value RLIKE '^[0-9]' THEN '' ELSE wp_postmeta.meta_value END ASC, wp_postmeta.meta_value+0 ASC";
return $orderby;
}
Before Query
add_filter( 'posts_orderby', 'filter_case' );
$wp_query = new WP_Query($args);
remove_filter( 'posts_orderby', 'filter_case' );