Home > Enterprise >  Get all posts except with specific post meta
Get all posts except with specific post meta

Time:05-04

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?

CodePudding user response:

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);
  • Related