Support » Fixing WordPress » How to show posts with a meta key on the top of the query results rest after tha
4 years, 1 month ago
I’m using WP_Query($args) to populate a search result of custom post types.
I want to sort the results based on a post meta value named offer_id.
Aim is to bring the posts with this meta key set to the top of the search results.
When I tried,
$args = .....
$args['meta_query'] = array(....);
'compare' => 'EXISTS',
$args['orderby'] = 'meta_value';
Only custom posts having offer_id meta value included in the results, posts without offer_is meta value are not included.
How can I show the posts with meta key offer_id on the top of the query results and posts without meta key on the bottom?
This requires the use of filters to modify the query. The article below shows some sample code that should get you started: http://wordpress.mcdspot.com/2013/01/25/sort-on-presence-of-custom-field-field-value-then-date/