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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:48:52  来源:igfitidea点击:

Query posts by custom taxonomy ID

wordpresstaxonomycustom-taxonomy

提问by mattberridge

I have a custom post type called portfolioand a custom taxonomy called build-type(acting as categories)

我有一个名为的自定义帖子类型portfolio和一个名为build-type(充当类别)的自定义分类法

I am trying to query portfolioposts by build-typeID e.g. all Portfolio posts in "Hotels" (id=4 for that taxonomy)

我正在尝试portfoliobuild-typeID查询帖子,例如“酒店”中的所有投资组合帖子(该分类法的 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 allportfolioposts and not just those with the build-typeID

目前它正在调用所有portfolio帖子,而不仅仅是那些有build-typeID 的帖子

For 'field' => 'term_id'should I be using term_id, tag_ID, idor something else?

对于'field' => 'term_id'我应该使用term_idtag_IDid或其他什么东西?

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-queryneeds 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 上:

https://gist.github.com/1275191

https://gist.github.com/1275191

回答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

//麦克风