admin管理员组文章数量:1333210
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>';
}
本文标签: Check the Specific Meta Key IF its Meta Value is Empty then… (WP Post Meta Query)
版权声明:本文标题:Check the Specific Meta Key IF its Meta Value is Empty then… (WP Post Meta Query) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742280825a2446052.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论