[英]WP_Query category parameter is not working
I am creating shortcode with attributes and all attributes working except event_category. 我正在创建具有属性和所有属性的短代码,但event_category除外。 When i add "event_category" attribute it is not giving result as per added category.
当我添加“ event_category”属性时,按照添加的类别它没有给出结果。
Here are my shortcode attributes 这是我的简码属性
// Shortcode Default Array
$default_shortcode_attrs = array(
'type' => 'upcoming',
'search' => 'true',
'event_category' => '',
'events_limit' => '-1',
);
extract(shortcode_atts($default_shortcode_attrs, $attr));
Following are query parameters 以下是查询参数
$args = array(
'posts_per_page' => -1,
'post_type' => 'event_listing',
'post_status' => 'publish',
'event_listing_category'=> $event_category,
);
$query = new WP_Query($args);
"event_listing_category" is name of custom taxonomy. “ event_listing_category”是自定义分类法的名称。 Please guide me why this query is not fetching the events according to their category.
请指导我为什么该查询不按事件类别提取事件。
Any help will be appreciated. 任何帮助将不胜感激。
Thanks 谢谢
Use tax_query instead as below: I'm assuming you have only one category slug provided in $event_category. 改用tax_query如下:我假设您在$ event_category中只提供了一个类别标签。 If you have more than one category slug in that variable then try converting it to array and replace the whole array($event_category) with the array.
如果该变量中有多个类别标签,则尝试将其转换为数组,然后将整个数组($ event_category)替换为数组。
$args = array(
'posts_per_page' => -1,
'post_type' => 'event_listing',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'event_listing_category',
'field' => 'slug',
'terms' => array( $event_category)
)
)
);
$query = new WP_Query($args);
See http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters on how to query for custom taxonomies within your query. 有关如何在查询中查询自定义分类法的信息,请参阅http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters 。
The WP_Query function doesn't understand 'event_listing_category'
as an argument, you'll have to tell wordpress that it's a custom taxonomy you want. WP_Query函数无法将
'event_listing_category'
理解为参数,您必须告诉wordpress这是您想要的自定义分类法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.