wordpress 按自定义分类 ID 查询帖子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7688591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Query posts by custom taxonomy ID
提问by mattberridge
I have a custom post type called portfolio
and a custom taxonomy called build-type
(acting as categories)
我有一个名为的自定义帖子类型portfolio
和一个名为build-type
(充当类别)的自定义分类法
I am trying to query portfolio
posts by build-type
ID e.g. all Portfolio posts in "Hotels" (id=4 for that taxonomy)
我正在尝试portfolio
按build-type
ID查询帖子,例如“酒店”中的所有投资组合帖子(该分类法的 id=4)
// gets the ID from a custom field to show posts on a specific page
$buildType = get_post_meta($post->ID, 'build_type_id', true);
// run query
query_posts(array(
'post_type' => 'portfolio',
'showposts' => -1,
'tax_query' => array(
'taxonomy' => 'build-type',
'terms' => $buildType,
'field' => 'term_id'
),
'orderby' => 'title',
'order' => 'ASC'
));
Currently it's calling allportfolio
posts and not just those with the build-type
ID
目前它正在调用所有portfolio
帖子,而不仅仅是那些有build-type
ID 的帖子
For 'field' => 'term_id'
should I be using term_id
, tag_ID
, id
or something else?
对于'field' => 'term_id'
我应该使用term_id
,tag_ID
,id
或其他什么东西?
Anyone know how to get this working?
有谁知道如何让这个工作?
Thanks in advance!
提前致谢!
回答by mattberridge
I solved it with help from: https://wordpress.stackexchange.com/questions/30476/query-posts-by-custom-taxonomy-id
我在以下帮助下解决了这个问题:https: //wordpress.stackexchange.com/questions/30476/query-posts-by-custom-taxonomy-id
tax-query
needs to be an array of arrays
tax-query
需要是一个数组数组
The final solution is:
最终的解决方案是:
// gets the ID from a custom field to show posts on a specific page
$buildType = get_post_meta($post->ID, 'build_type_id', true);
// run query
query_posts(array(
'post_type' => 'portfolio',
'showposts' => -1,
'tax_query' => array(
array(
'taxonomy' => 'build-type',
'terms' => $buildType,
'field' => 'term_id',
)
),
'orderby' => 'title',
'order' => 'ASC' )
);
On github here:
在 github 上:
回答by Mike
I'm not a WP-gury and I have invested hours and hours trying to solve the same problem. Eventually I found this blog post: http://richardsweeney.com/blog/wordpress-3-0-custom-queries-post-types-and-taxonomies/
我不是 WP-gury,我已经投入了数小时和数小时试图解决同样的问题。最终我找到了这篇博文:http: //richardsweeney.com/blog/wordpress-3-0-custom-queries-post-types-and-taxonomies/
The answer is somewhat semi-bad: apparently you can't filter like this for custom post types (it is only possible for posts), which is a shame!
答案有点糟糕:显然你不能像这样过滤自定义帖子类型(只能用于帖子),这是一种耻辱!
What I did work was this:
我所做的工作是这样的:
$args['custom_tax'] = 'custom_tax_slug'; query_posts($args);
$args['custom_tax'] = 'custom_tax_slug'; query_posts($args);
Hope it helps!
希望能帮助到你!
//Mike
//麦克风