我有一个自定义帖子类型portfolio
和一个自定义分类法build-type
(作为类别)
我试图通过build-type
ID查询portfolio
帖子,例如"酒店"中的所有组合帖子(该分类法的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'
));
目前它调用所有 portfolio
帖子,而不仅仅是那些具有build-type
ID的帖子
对于'field' => 'term_id'
我应该使用term_id
, tag_ID
, id
或其他东西吗?
有人知道怎么让它工作吗?
提前感谢!
我在https://wordpress.stackexchange.com/questions/30476/query-posts-by-custom-taxonomy-id的帮助下解决了这个问题
tax-query
需要是一个数组的数组
最终的解决方案是:
// 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' )
);
在github:
https://gist.github.com/1275191我不是一个wp爱好者,我花了很多时间试图解决同样的问题。最后我找到了这篇博文:http://richardsweeney.com/blog/wordpress-3-0-custom-queries-post-types-and-taxonomies/
答案有点糟糕:显然你不能像这样过滤自定义帖子类型(它只可能用于帖子),这是一个耻辱!
我所做的工作是这样的:
$args['custom_tax'] = 'custom_tax_slug';query_posts (args);
希望有帮助!
迈克//