3

我已经为多种帖子类型注册了分类法,认为这是设置网站而不是重复相同的分类法的最佳方式。

但是,现在我遇到了一个问题,我需要列出一个帖子类型的使用分类法,但它列出了所有两种类型的分类法。我该如何解决这个问题?get_categories 或 get_terms 似乎都有一个选项来指定您想要获取分类法的帖子类型。

编辑 注意:每个帖子类型也有多个分类法

任何人都可以帮忙吗?

register_taxonomy( 
        'sectors',
        array('case-study', 'resource'),   //used in multiple post types
        [
            'labels' => [
                'name' => __( 'Sectors' ),
                'singular_name' => __( 'Sector' ),
            ],
            'hierarchical' => true,
            'show_admin_column' => true,
        ]
    );

    $sectors = get_categories( array('taxonomy' => 'sectors') );  //prints out selected taxonomies for both case studies and resources when I want just resources.

   $services = get_categories( array('taxonomy' => 'services') );  
4

3 回答 3

1

我发现下面的代码就像一个魅力:)

function df_terms_clauses( $clauses, $taxonomy, $args ) {
    if ( isset( $args['post_type'] ) && ! empty( $args['post_type'] ) && $args['fields'] !== 'count' ) {
        global $wpdb;

        $post_types = array();

        if ( is_array( $args['post_type'] ) ) {
            foreach ( $args['post_type'] as $cpt ) {
                $post_types[] = "'" . $cpt . "'";
            }
        } else {
            $post_types[] = "'" . $args['post_type'] . "'";
        }

        if ( ! empty( $post_types ) ) {
            $clauses['fields'] = 'DISTINCT ' . str_replace( 'tt.*', 'tt.term_taxonomy_id, tt.taxonomy, tt.description, tt.parent', $clauses['fields'] ) . ', COUNT(p.post_type) AS count';
            $clauses['join'] .= ' LEFT JOIN ' . $wpdb->term_relationships . ' AS r ON r.term_taxonomy_id = tt.term_taxonomy_id LEFT JOIN ' . $wpdb->posts . ' AS p ON p.ID = r.object_id';
            $clauses['where'] .= ' AND (p.post_type IN (' . implode( ',', $post_types ) . ') OR p.post_type IS NULL)';
            $clauses['orderby'] = 'GROUP BY t.term_id ' . $clauses['orderby'];

            //print_r( $clauses );
        } 
    }
    return $clauses;
}

add_filter( 'terms_clauses', 'df_terms_clauses', 10, 3 );
于 2018-04-09T16:18:59.633 回答
1

尝试这个

<?php

     $custom_terms = get_terms('custom_taxonomy_name');

foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array('post_type' => 'custom_post_type_name',
        'tax_query' => array(
            array(
                'taxonomy' => 'custom_taxonomy_name',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
     );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        echo '<h2>'.$custom_term->name.'</h2>';

        while($loop->have_posts()) : $loop->the_post();
            echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';
        endwhile;
     }
}
?>
于 2018-04-09T14:21:44.217 回答
0
$terms = get_terms( 'animal_cat', array(
    'orderby'    => 'count',
    'hide_empty' => 0
) );
foreach( $terms as $term ) {

    // Define the query
    $args = array(
        'post_type' => 'animal',
        'animal_cat' => $term->slug
    );
    $query = new WP_Query( $args );

}
于 2018-04-09T11:34:07.177 回答