How to show posts with a meta key on the top of the query results rest after tha (2 posts)

  1. Mithun Sreedharan
    Posted 2 years 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(....);
    $args['meta_query']['offers'] =
        '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?

  2. vtxyzzy
    Posted 2 years ago #

    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/

Topic Closed

This topic has been closed to new replies.

About this Topic