Home > Blockchain >  How to sort WP Query by meta key exist or that meta value is equal one
How to sort WP Query by meta key exist or that meta value is equal one

Time:10-31

I have filter page which is contains all the product list, And i have two option first once is latest another one is featured. In database the post meta storing as is_feature = 1. During wp_query its only fetching is feautre =1. Actually i need data as like is_feature = 1 from starting ,which post doesn't have is_feature = '' or doesn't is_feature meta key, they need to display later is_feature = 1.

This is my query:

    $conditions = array( 
              'posts_per_page' => 9, 
              'paged' => $paged,   
              'post_type'   => 'custom',
              'meta_key' => 'is_feature',
              'order_by' => 'meta_key=is_feature',
              'order'            => 'DESC',
              'post_status' => "publish", )


    $the_query = new WP_Query( $conditions ); 

CodePudding user response:

Replace order_by to orderby. Use meta_value as orderby parameter:

$conditions = array( 
    'posts_per_page' => 9, 
    'paged'          => $paged,   
    'post_type'      => 'custom',
    'meta_key'       => 'is_feature',
    'orderby'        => 'meta_value',
    'order'          => 'DESC',
    'post_status'    => "publish"
);

https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

  • Related