I was wondering if you’d be able to help with an issue i’m having?
I have implemented a fix that you came up with in this post:
How to order custom post type by multiple custom fields? however when I add the filter no posts are being displayed.
What I’m trying to achieve is to order a league table by points and then by goal difference.
This is my function in functions.php
function orderbyreplace($orderby ) {
global $wpdb;
return str_replace($wpdb->prefix.'postmeta.meta_value', 'mt1.meta_value, mt2.meta_value ASC', $orderby);
}
And here’s the query in my template file
$args = array(
'post_type'=>'standings',
'orderby' => 'meta_value_num',
'meta_key' => '_pts',
'meta_query' => array(
array(
'key' => '_pts',
'value' => '0',
'type' => 'NUMERIC',
'compare' => '!='
),
array(
'key' => '_dif',
'value' => '0',
'type' => 'NUMERIC',
'compare' => '!='
)
),
);
add_filter('posts_orderby','orderbyreplace');
$loop = new WP_Query( $args );
remove_filter('posts_orderby','orderbyreplace');
If you could spare a few minutes of your time to have a look over this and make any suggestions you may have and point anything out where I might have gone wrong that would be greatly appreciated.
EDIT
I’ve changed the code to the following and it is know returning posts however it’s only ordering them by the first meta_key and ignores the other.
function orderbyreplace($orderby) {
global $wpdb;
return str_replace($wpdb->prefix.'postmeta.meta_value_num', 'mt1.meta_value, mt2.meta_value DESC', $orderby);
}
$args = array(
'post_type'=>'standings',
'orderby' => 'meta_value_num',
'meta_key' => '_pts',
'meta_query' => array(
array(
'key' => '_pts',
),
array(
'key' => '_dif',
)
),
);
add_filter('posts_orderby','orderbyreplace');
$loop = new WP_Query( $args );
remove_filter('posts_orderby','orderbyreplace');
Many thanks,
Ben