I have many posts. Some have meta data, with a key called "foo".
How can I query for just the posts with the meta data "foo"? metadata_exists requires the post ID, I'm trying to get the post IDs!
I have many posts. Some have meta data, with a key called "foo".
How can I query for just the posts with the meta data "foo"? metadata_exists requires the post ID, I'm trying to get the post IDs!
I think something like this should do the job:
$meta_query = array(
array(
'key' => 'foo',
'compare' => 'EXISTS',
),
);
$args = array(
'meta_query' => $meta_query
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$post_id = get_the_ID();
}
} else {
// no posts found
}
wp_reset_postdata();
meta_query
parameter. The docs on Custom Fields has some helpful examples.