Note that the correct compare
value is EXISTS
and not EXIST
. post_id
is also not a valid/standard parameter for a meta query clause. 🙂
But 'compare' => 'EXIST'
is equivalent to 'compare' => '='
, i.e. the default compare value is =
, therefore your meta query worked and only posts with the meta (regardless the value is empty or not) included in the results.
And actually, you don’t need the meta query there if you just want to check if a post contains a specific meta.
Just use get_post_meta()
to get the meta value and then do an if-else check like in the while
loop below:
$args = array(
'post_type' => 'post',
);
// Get all matching posts, with and without the meta wc_pay_per_post_product_ids.
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
// Get the meta value.
$meta = get_post_meta( get_the_ID(), 'wc_pay_per_post_product_ids', true );
// Then do an if-else:
if ( $meta ) {
echo 'has meta<br>';
} else {
echo 'has no meta<br>';
}
endwhile;
You can also use metadata_exists()
if you don’t need to access/know the meta value:
if ( metadata_exists( 'post', get_the_ID(), 'wc_pay_per_post_product_ids' ) ) {
echo 'has meta<br>';
} else {
echo 'has no meta<br>';
}