如何获取自定义分类术语的帖子

时间:2015-06-29 作者:Ebenizer Pinedo

我希望有人能帮助我:

我有一个Custom Post Type (电影)及其custom taxonomy (生产者),此分类法有其own terms, 例如“WarnerBros”。

我怎样才能得到我任期内的所有职位(WarnerBros)?

我有这个,但还没用。

$args = array(
   \'post_type\' => \'movie\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'producer\',
            \'field\'    => \'slug\',
            \'terms\'    => \'WarnerBros\',
        ),
    ),
);
$query = new WP_Query( $args );
在使用解决问题的代码后,我将与具有相同问题的人共享我的代码:

 
$type = \'Movie\';  // Custom Post Type Name
$tag =  \'WarnerBros\'; // Your Term

$args = array( \'post_type\' => $type, \'paged\' => $paged, \'posts_per_page\' => -1, \'orderby\' => \'menu_order\', \'order\' => \'ASC\', \'tax_query\'=>array( array( \'taxonomy\'=>\'Producer\', //Taxonomy Name \'field\'=>\'slug\', \'terms\'=>array($tag) )) );

$loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); if(is_object_in_term($post->ID,\'Taxonomy_Name\',\'Your_Term\')) // Producer and WarnerBros {

echo \'<div id="YourID">\'; echo the_title(); echo \'</div>\'; } endwhile;

3 个回复
SO网友:marcovega

这个问题在这个特定的Wordpress问题中有不同的答案,它们可能会有所帮助:

Display all posts in a custom post type, grouped by a custom taxonomy

就我个人而言,我使用的这种方法对我来说很好:

$terms = get_terms(\'tax_name\');
$posts = array();
foreach ( $terms as $term ) {
    $posts[$term->name] = get_posts(array( \'posts_per_page\' => -1, \'post_type\' => \'post_type\', \'tax_name\' => $term->name ));
}
将其编辑到您的场景中应该可以:

$terms = get_terms(\'producer\');
$posts = array();
foreach ( $terms as $term ) {
    $posts[$term->name] = get_posts(array( \'posts_per_page\' => -1, \'post_type\' => \'movie\', \'tax_name\' => $term->name ));
}
现在您可以获得您的帖子:

print_r($posts["WarnerBros"]);

SO网友:Iftieaq

像这样试试

$args = array(
    \'post_type\' => \'movie\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'producer\',
            \'field\'    => \'slug\',
            \'terms\'    => \'WarnerBros\',
        ),
    ),
);
$query = new WP_Query( $args );
更多信息,请访问wordpress codex

SO网友:Suit Boy Apps

假设您有一个自定义的post类型plays 在分类法下genre 您想查找该类别的所有帖子comedy

$args = array(
        \'post_type\' => \'plays\', /*Post type (plays)*/
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'genre\', /*Taxonomy to search (genre)*/
                \'field\'    => \'slug\',
                \'terms\'    => \'comedy\', /*Search category for (comedy)*/
            ),
        ),
    );
    $query = new WP_Query( $args );

结束

相关推荐