[英]Querying and displaying posts in taxonomy, custom post type
我有一个名为resources
的自定义帖子类型:
register_post_type(
'resources',
tp_build_post_args(
'resources', 'Resource', 'Resources',
array(
'menu_icon' => 'dashicons-welcome-write-blog',
'menu_position' => 20,
'public' => true,
'supports' => array('editor', 'title','author','thumbnail', 'revisions'),
'taxonomies' => array('sector', 'subject', 'type'),
)
)
);
还有一个称为type
的分类法:
register_taxonomy(
'type',
'type',
array(
'hierarchical' => true,
'label' => 'Type',
'query_var' => true,
'rewrite' => array(
'slug' => 'type',
'with_front' => false
)
)
);
类型可以是以下之一:
我正在尝试使用wp_query
显示“新闻” type
的resources
下的所有帖子。 但是,它会返回所有帖子。
我的方法在哪里分崩离析:
$card_count = 4;
$resource_type = 'News';
$args = array(
'post_type' => 'resources',
'post_status' => 'publish',
'posts_per_page' => $card_count,
//'cat' => $resource_type,
'tax_query' => array(
array(
'taxonomy' => 'type',
'terms' => $resource_type,
)
)
);
试试看:
'tax_query' => array( array( 'taxonomy' => 'type', 'terms' => $resource_type, 'field' => 'name', // or 'slug' 'operator' => 'IN' ) ),
我的猜测是您需要指定您要查找的是分类的name
,默认情况下它可能正在查找ID
。
$args = array(
'post_type' => 'resources',
'post_status' => 'publish',
'posts_per_page' => $card_count,
//'cat' => $resource_type,
array(
'taxonomy' => 'type',
'terms' => $resource_type,
'field' => 'name',
'operator' => 'IN',
'include_children' => true,
)
);
您可以阅读文档以获得更好的理解 - 或者如果您想作弊,这是一个不错的生成器: https://generatewp.com/wp_tax_query/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.