通过自定义分类ID查询帖子

发布于 2024-12-08 19:47:41 字数 887 浏览 4 评论 0原文

我有一个名为 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_idtag_IDid 还是其他?

有人知道如何让它工作吗?

提前致谢!

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不醒的梦 2024-12-15 19:47:41

我在以下帮助下解决了这个问题: 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

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 arrays

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:

https://gist.github.com/1275191

时光病人 2024-12-15 19:47:41

我不是 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文