通过自定义分类ID查询帖子
我有一个名为 portfolio
的自定义帖子类型和一个名为 build-type
的自定义分类法(充当类别)
我试图通过以下方式查询 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'
));
目前它正在调用所有 portfolio
帖子,而不仅仅是那些与build-type
ID
对于'field' => 'term_id'
我应该使用 term_id
、tag_ID
、id
还是其他?
有人知道如何让它工作吗?
提前致谢!
I have a custom post type called portfolio
and a custom taxonomy called build-type
(acting as categories)
I am trying to query portfolio
posts by build-type
ID e.g. all Portfolio posts in "Hotels" (id=4 for that taxonomy)
// 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 all portfolio
posts and not just those with the build-type
ID
For 'field' => 'term_id'
should I be using term_id
, tag_ID
, id
or something else?
Anyone know how to get this working?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在以下帮助下解决了这个问题: https://wordpress.stackexchange.com/questions /30476/query-posts-by-custom-taxonomy-id
tax-query
需要是数组的数组最终的解决方案是:
在 github 上:
https://gist.github.com/1275191
I solved it with help from: https://wordpress.stackexchange.com/questions/30476/query-posts-by-custom-taxonomy-id
tax-query
needs to be an array of arraysThe final solution is:
On github here:
https://gist.github.com/1275191
我不是 WP-gury,但我投入了很多时间来尝试解决同样的问题。最终我找到了这篇博文: http:// /richardsweeney.com/blog/wordpress-3-0-custom-queries-post-types-and-taxonomies/
答案有点半坏:显然你不能像这样过滤自定义帖子类型(它只能用于帖子),这是一种耻辱!
我所做的工作是这样的:
$args['custom_tax'] = 'custom_tax_slug';
query_posts($args);
希望有帮助!
//麦克风
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/
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);
Hope it helps!
//Mike