'Get all posts except with specific post meta
So I have the following conditional:
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 50,
'paged' => $page,
'orderby' => 'date',
'order' => 'desc',
'meta_query' => [
[
'key' => 'global_post',
'compare' => '!=',
]
],
];
$query = new WP_Query($args);
I have some posts that have the post_meta as 'global_post' - I want to be able to pull ALL posts except posts that have that specific post_meta.
With the above code, I keep getting an empty/null return, even if I have posts with and without that specific post_meta. What am I doing wrong?
Solution 1:[1]
Have you tried NOT EXISTS for the compare? Would look like this:
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 50,
'paged' => $page,
'orderby' => 'date',
'order' => 'desc',
'meta_query' => [
[
'key' => 'global_post',
'compare' => 'NOT EXISTS',
]
],
];
$query = new WP_Query($args);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mikerojas |
