Reputation: 23
Following situation: I have a custom post type called picture in Wordpress. You can rate each picture by either liking or disliking it. The result of these votes is saved as meta values in the wp_postmeta table. It consists of 3 meta_values for each picture: rating ratio in percent, sum of likes and sum of dislikes
Now what I want to do, is creating a WP_Query which gets me the pictures first ordered by rating ratio and if the rating ratio is the same for more than one picture it should order by sum of likes. Each order criteria DESC of course.
I tried the following (beside many other versions that did not work):
$args = array('post_type' => 'picture',
'posts_per_page' => 10,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => 'rating_ratio',
'meta_query' => array(
array(
'key' => 'rating_ratio',
'value' => '0',
'type' => 'NUMERIC',
'compare' => '!='
),
array(
'key' => 'likes',
'value' => '0',
'type' => 'NUMERIC',
'compare' => '!='
)
)
);
This somehow only orders by rating_ratio. If I could have a look at the SQL-Query that Wordpress generates, I might have a chance to find the right parameters here, but $GLOBALS['wp_query']->request
only shows me the main query.
Help is much appreciated.
UPDATE:
@meet: Thx. I tried your input. Now I get the best result with the following query:
$args = array('post_type' => 'picture',
'posts_per_page' => 10,
'orderby' => 'meta_value_num meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'rating_ratio',
'value' => $rating_ratio,
'compare' => 'LIKE'
),
array(
'key' => 'likes',
'value' => $likes,
'compare' => 'LIKE'
)
)
);
(Without the orderby => meta_value_num
it's just sorting by date.) The problem with this new version still is, that it doesn't really order the posts by likes. It only orders them correctly by rating_ratio and then it seems to be more or less random.
An example: I have several posts with rating_ratio=100
(all correctly ordered above the ones with rating_ratio<100
) where the ones with likes=4
are falsely ordered above the one with likes=5
.
FINAL WORKING SOLUTION
$args =
array( 'post_type' => 'picture',
'posts_per_page' => 10,
'meta_query' => array(
'relation' => 'AND',
'rating_ratio_clause' => array(
'key' => 'rating_ratio',
'type' => 'NUMERIC',
),
'likes_clause' => array(
'key' => 'likes',
'type' => 'NUMERIC',
)
),
'orderby' => array(
'rating_ratio_clause' => 'DESC',
'likes_clause' => 'DESC'
)
);
Upvotes: 2
Views: 2803
Reputation: 404
Try using this example
$q = new WP_Query( array(
'meta_query' => array(
'relation' => 'AND',
'state_clause' => array(
'key' => 'state',
'value' => 'Wisconsin',
),
'city_clause' => array(
'key' => 'city',
'compare' => 'EXISTS',
),
),
'orderby' => 'city_clause', // Results will be ordered by 'city' meta values.
) );
And For multiple use this,
'orderby' => array(
'city_clause' => 'ASC',
'state_clause' => 'DESC',
),
Upvotes: 2
Reputation: 404
Try this.
$args = array(
'post_type' => 'dt_materials',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'materials',
'value' => $materials,
'compare' => 'LIKE'
),
array(
'key' => 'size',
'value' => $size,
'compare' => 'LIKE'
),
) );
Upvotes: 0