Wordpress - 在多种帖子类型上使用的分类法 - 如何只为一个get_terms / get_categories?

时间:2018-04-09 09:39:23

标签: php wordpress taxonomy-terms

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

但是,现在我遇到了一个问题,我需要列出一个帖子类型的已使用的分类法,但是它列出了所有这两种类型的列表分类法。我该如何解决这个问题? Niether 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') );  

3 个答案:

答案 0 :(得分:1)

试试这个

public class Variable implements Parcelable {
    public View mView;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeValue(mView)
}

public static final Parcelable.Creator<Variable> CREATOR
        = new Parcelable.Creator<Variable>() {
    public Variable createFromParcel(Parcel in) {
        return new Variable(in);
    }

    public Variable[] newArray(int size) {
        return new Variable[size];
    }
};

private Variable(Parcel in) {
   mView= (View) in.readValue(getClass().getClassLoader());
}

答案 1 :(得分: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 );

答案 2 :(得分: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 );

}