admin管理员组文章数量:1125605
I am using a custom post-type, in post there are a checkbox meta field for featured post.
So there are two scenarios.
- First with featured posts
- Second with non featured
When I called featured posts with meta_query
its result 100% perfect.
Here is my code:
$c_clients_args = array(
'post_type' => 'clients_testimonials',
'posts_per_page' => 6,
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'c_client_feature',
'value' => 'on'
)
)
);
$c_clients_result = new WP_Query($c_clients_args);
if($c_clients_result->have_posts()) :
while($c_clients_result->have_posts()) :
$c_clients_result->the_post();
// My result
endwhile;
endif;
But when I call non featured posts with meta_query
the Page goes blank.
Here is my code:
$c_client_args = array(
'post_type' => 'clients_testimonials',
'posts_per_page' => 999,
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'c_client_feature',
'value' => 'on',
'compare' => 'NOT LIKE'
)
)
);
I also tried !=
and NOT IN
So please guide me.
I am using a custom post-type, in post there are a checkbox meta field for featured post.
So there are two scenarios.
- First with featured posts
- Second with non featured
When I called featured posts with meta_query
its result 100% perfect.
Here is my code:
$c_clients_args = array(
'post_type' => 'clients_testimonials',
'posts_per_page' => 6,
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'c_client_feature',
'value' => 'on'
)
)
);
$c_clients_result = new WP_Query($c_clients_args);
if($c_clients_result->have_posts()) :
while($c_clients_result->have_posts()) :
$c_clients_result->the_post();
// My result
endwhile;
endif;
But when I call non featured posts with meta_query
the Page goes blank.
Here is my code:
$c_client_args = array(
'post_type' => 'clients_testimonials',
'posts_per_page' => 999,
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'c_client_feature',
'value' => 'on',
'compare' => 'NOT LIKE'
)
)
);
I also tried !=
and NOT IN
So please guide me.
Share Improve this question asked Apr 27, 2016 at 7:07 deemi-D-nadeemdeemi-D-nadeem 5672 gold badges8 silver badges26 bronze badges 10 | Show 5 more comments2 Answers
Reset to default 1Issue with your second query for non-featured posts is that 'NOT LIKE'
and '!='
comparisons might not be working as expected for a checkbox field. For checkbox fields in WordPress, you should generally use 'NOT EXISTS'
or '!= 1'
in the meta_query
try this second query
$c_client_args = array(
'post_type' => 'clients_testimonials',
'posts_per_page' => 999,
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'c_client_feature',
'value' => 'on',
'compare' => 'NOT EXISTS', // Use 'NOT EXISTS' for checkbox fields
),
),
);
$c_client_result = new WP_Query($c_client_args);
try this second query
$c_client_args = array(
'post_type' => 'clients_testimonials',
'posts_per_page' => 999,
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'c_client_feature',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'c_client_feature',
'value' => 'on',
'compare' => 'NOT IN'
),
)
);
本文标签: custom fieldmetaquery 39compare39 gt 3939 not working
版权声明:本文标题:custom field - meta_query 'compare' => '!=' not working 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736667355a1946733.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
nopaging => true
andposts_per_page => -1
. – cybmeta Commented Apr 27, 2016 at 7:44c_client_feature
meta field exists for non featured posts? – cybmeta Commented Apr 27, 2016 at 7:46