Prioritizing wp_query by meta key
-
I have two custom fields for views.
weekly_views
andall_views
. Theweekly_views
custom field is deleted every week and starts counting views again from 0. So now what I want to achieve is show 12 posts byweekly_views
but when the custom field is deleted and unless there are views on those posts the query shows nothing. I want to show here posts byall_views
instead of no posts.My query goes as follows but it’s not working as I want. In short what I want to achieve is to show posts by
weekly_views
custom field but if there’s no post then show posts byall_views
. And also if there’s less than 12 posts byweekly_views
then showweekly_views
posts first and then remaining posts byall_views
.$args = array( 'post_type' => array( 'custom_post_type_1', 'custom_post_type_2'), 'posts_per_page' => '12', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'weekly_views', ), array( 'key' => 'all_views', ), ), );
The above code is returning me posts but are sorted by all_views.
- The topic ‘Prioritizing wp_query by meta key’ is closed to new replies.