简体   繁体   中英

How can I show all posts along with meta query in WP_Query?

I've a custom query which is displaying posts which is only has meta key value. But I want to show all other posts that has no value in the meta key.

Here is my code you can see -

$today = date('Ymd');

$_press_release = new WP_Query(array(
    'post_type'         =>  'press_release',
    'meta_key'          =>  'press_release_date',
    'orderby'           =>  'meta_value_num',
    'meta_query'        =>  array(
        array(
            'key'       =>  'press_release_date',
            'compare'   =>  '<=',
            'value'     =>  $today,
            'type'      =>  'numeric'
        ),
    )
));

This query is basically stands for sorting posts based on the press release date meta key. But for some posts has no date in this meta field. Those posts is not showing in this query.

I just want to show all the posts that has the date will display first, and then other posts that has no value in the respective meta field will display then.

I've already tried with 'posts_per_page' => -1 but it's not showing all other posts. The query is only working for those posts which is only has the press_release_date meta key value.

How can I achieve this?

Thanks.

use this code

   'posts_per_page'   => -1,
$today = date('Ymd');

$_press_release = new WP_Query(array(
    'post_type'         =>  'press_release',
    'meta_key'          =>  'press_release_date',
   'posts_per_page'   => -1,
    'orderby'           =>  'meta_value_num',
    'meta_query'        =>  array(
        array(
            'key'       =>  'press_release_date',
            'compare'   =>  '<=',
            'value'     =>  $today,
            'type'      =>  'numeric'
        ),
    )
));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM