0

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.

Saif
  • 3
  • 4
  • You will probably have to add a `NOT EXISTS` query to your meta query, similar as suggested in https://stackoverflow.com/a/72414243/1427878 – CBroe Nov 24 '22 at 11:13
  • https://prnt.sc/aVAqIbX_uzQe I've already tried with this query. But it's not working for me. – Saif Nov 24 '22 at 11:23
  • First of all, [Please do not upload images of code/data/errors.](//meta.stackoverflow.com/q/285551) (add this to your question in text form instead, properly formatted), and second, "not working" is not a proper problem description. Please go read [ask]. Explain how it actually behaves - do you get too many posts, too little, none at all, did you get errors, did your computer explode ... – CBroe Nov 24 '22 at 11:26
  • I apologize. I got only those posts that has the meta key value. But I want to show all posts that has press release date value first, and then other posts will show in the loop. – Saif Nov 24 '22 at 11:30
  • Does the meta table entry for the key for those posts actually not exist; or does it perhaps exist, but has an empty string or NULL as value? – CBroe Nov 24 '22 at 11:34
  • It exists and it has an empty value if there is no input given. But I'm not getting those posts which has empty value in this key. – Saif Nov 24 '22 at 11:40
  • Well if it exists, then you must of course not use `NOT EXISTS`. And passing `'meta_key' => 'press_release_date'` _and_ your `meta_query`, I think that also makes no sense here. Remove the former. – CBroe Nov 24 '22 at 11:44
  • ```meta_query``` is for serializing posts according to the ```press_release_date``` field value. If I remove it then How can I serialize the posts according to their specific date, not published date – Saif Nov 24 '22 at 11:48
  • I did not say remove the `meta_query`, I said remove `meta_key`. – CBroe Nov 24 '22 at 11:49
  • No changes. It does the same as it was before. – Saif Nov 24 '22 at 11:52
  • https://prnt.sc/Hlqyyg2giWTa This is the date field in dashboard. I want to serialize all posts based on this date. The post loop will return the posts by serialize like this date - 30 Nov 2022, 29 Nov 2022, 30 May 2021 etc – Saif Nov 24 '22 at 11:55
  • It requires having a meta key exist in the database with an empty value when you use sort by that meta key, if the meta key doesn't exist with an empty value then those posts will not show in your query. I don't know why this behavior happens but this is a thing. – Vijay Hardaha Nov 24 '22 at 13:18

1 Answers1

-1

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'
        ),
    )
));
Ashok kumawat
  • 492
  • 3
  • 15
  • 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. – Saif Nov 24 '22 at 11:12
  • https://prnt.sc/4MpzK3wbX1s7 then increase limit form here – Ashok kumawat Nov 24 '22 at 11:13
  • https://prnt.sc/exrH2vGhg1bW All posts has loaded there. Pagination is also there. But It's showing only those posts which has the meta key value. Not working even after increasing the ```Blog pages show at most``` value from settings. – Saif Nov 24 '22 at 11:16
  • What format you have in db of meta? – Ashok kumawat Nov 28 '22 at 08:13