WordPress – get posts/pages with missing meta key

Get post_type page with missing meta_key

$query = array(
    'posts_per_page' => -1,
    'post_type'    => 'page',
    'post_status'  => 'publish',
    'post_parent' => 'OPTIONAL_PAGE_PARENTID',
);
$query['meta_query'] = array();
$query['meta_query'][] = array(
     'key' => 'KEYNAME',
     'compare' => 'NOT EXISTS',
);
$posts = get_posts($query);

Get post_type post with missing meta_key

$query = array(
    'posts_per_page' => -1,
    'post_type'    => 'post',
    'post_status'  => 'publish',
);
$query['meta_query'] = array();
$query['meta_query'][] = array(
     'key' => 'KEYNAME',
     'compare' => 'NOT EXISTS',
);
$posts = get_posts($query);
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments