[英]WP_Query get the results weather selected or not
我正在与WP_Query一起使用来自自定义帖子类型的多个taxonomy
从数据库中提取结果。我有两个分类法,每个分类法都下拉了。分类法city
和cuisine
但如果不选择它们中的任何一个,则不会显示结果。即使未选择这些分类法,我也只想显示关键词结果。
$args = array(
'post_type' => 'listings',
's' => get_query_var( 's' ),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'cuisine',
'field' => 'slug',
'terms' => $selected_cuisine,
'relation' => 'AND',
),
array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => $selected_city,
'relation' => 'AND',
),
),
);
$restaurant_query = new WP_Query( $args );
您的查询以哪种方式知道何时选择了分类术语? 您可以稍后尝试使用if
语句添加tax_query
:
if ($term == 'cuisine') {
$args['meta_query'][] = array(
'taxonomy' => 'cuisine',
'field' => 'slug',
'terms' => $selected_cuisine,
'relation' => 'AND',
);
} elsif($term == 'city') {
$args['meta_query'][] = array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => $selected_city,
'relation' => 'AND',
);
}
这就是为什么仅在选择term
时才添加tax_query
原因。 过去,我在此问题上遇到过一些奇怪的问题,这种方法已经多次解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.