我有一个循环,我正在使用自定义分类法构建过滤器。
它有效,但仅在输入值时才有效。当页面加载时,它不显示任何条目,但是当我按值过滤时,它会显示带有该标记的项目。我以为那就是喜欢'喜欢'允许它在值为空时显示所有条目。
我已经尝试过删除$成分并将其替换为空的'
。<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'ingredients',
'value' => $ingredients,
'compare' => 'LIKE'
)
)
);
$query = new WP_Query($args);
while($query->have_posts()) : $query->the_post();?>
<?php the_title(); ?><br />
<?php endwhile; wp_reset_query(); ?>
答案 0 :(得分:2)
通过将false传递给参数
进行检查$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'ingredients',
'value' => $ingredients,
'compare' => 'LIKE'
),
array(
'key' => 'ingredients',
'value' => false,
'type' => 'BOOLEAN'
),
)
);
另外添加compare =&gt;喜欢虚假的情况和交叉检查。