I'm trying to make a wp query to WP Post Meta Table where I want to check the post's META KEY and its META VALUE if its EMPTY or NOT.
The meta key is wc_pay_per_post_product_ids.
IF the statement is TRUE, display the following:
<h1>The Meta Key Is Empty or NULL</h1>
<div class="row"><div class="col-2">One</div><div class="col-10">Two</div></div>
ELSE:
<h1>The Meta Key Is NOT Empty or NULL</h1>
<div class="row"><div class="col-4"></div><div class="col-4"></div><div class="col-4"></div></div>
Here's the complete code.
<?php
global $wpdb;
$post_id = get_the_ID();
$meta_value = 'wc_pay_per_post_product_ids';
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'post_id' => get_the_ID(),
'key' => 'wc_pay_per_post_product_ids',
'compare' => 'EXIST' // CHECK THE VALUE OF META KEY IF EXISTS?
)
)
);
if ($arg == TRUE) {
echo '<h1>The Meta Key Is Empty or NULL</h1><div class="row"><div class="col-2"></div><div class="col-10"></div></div>';
} else {
echo '<h1>The Meta Key Is NOT Empty or NULL</h1><div class="row"><div class="col-4">ONE</div><div class="col-4">TWO</div><div class="col-4">THREE</div></div>';
}
?>
Here's the screenshot of my table:
The issue I'm encountering is it always return the result "The Meta Key Is NOT Empty or NULL" Any idea what's wrong?
I'm trying to make a wp query to WP Post Meta Table where I want to check the post's META KEY and its META VALUE if its EMPTY or NOT.
The meta key is wc_pay_per_post_product_ids.
IF the statement is TRUE, display the following:
<h1>The Meta Key Is Empty or NULL</h1>
<div class="row"><div class="col-2">One</div><div class="col-10">Two</div></div>
ELSE:
<h1>The Meta Key Is NOT Empty or NULL</h1>
<div class="row"><div class="col-4"></div><div class="col-4"></div><div class="col-4"></div></div>
Here's the complete code.
<?php
global $wpdb;
$post_id = get_the_ID();
$meta_value = 'wc_pay_per_post_product_ids';
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'post_id' => get_the_ID(),
'key' => 'wc_pay_per_post_product_ids',
'compare' => 'EXIST' // CHECK THE VALUE OF META KEY IF EXISTS?
)
)
);
if ($arg == TRUE) {
echo '<h1>The Meta Key Is Empty or NULL</h1><div class="row"><div class="col-2"></div><div class="col-10"></div></div>';
} else {
echo '<h1>The Meta Key Is NOT Empty or NULL</h1><div class="row"><div class="col-4">ONE</div><div class="col-4">TWO</div><div class="col-4">THREE</div></div>';
}
?>
Here's the screenshot of my table:
The issue I'm encountering is it always return the result "The Meta Key Is NOT Empty or NULL" Any idea what's wrong?
Share Improve this question asked Jul 9, 2020 at 3:56 Ben Michael OracionBen Michael Oracion 1051 silver badge5 bronze badges1 Answer
Reset to default 2Note 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>';
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742280826a4414446.html
评论列表(0条)