Wordpress - 分类法用于多种 post 类型 - 如何 get_terms / get_categories 只用于一种类型?
Wordpress - taxonomy used on multiple post types - how to get_terms / get_categories for just one?
我已经为多个 post 类型注册了分类法,认为这是设置网站的最佳方式,而不是重复相同的分类法。
但是,现在我 运行 遇到了一个问题,我需要列出 post 类型使用的分类法,但列出所有这两种类型的分类法。我该如何解决这个问题? Niether get_categories 或 get_terms 似乎有一个选项可以指定你想要获取哪个 post 类型的分类法。
编辑
注意:每个 post 类型也有多个分类法
有人能帮忙吗?
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') );
$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 );
}
试试这个
<?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;
}
}
?>
我发现下面的代码很有魅力:)
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 );
我已经为多个 post 类型注册了分类法,认为这是设置网站的最佳方式,而不是重复相同的分类法。
但是,现在我 运行 遇到了一个问题,我需要列出 post 类型使用的分类法,但列出所有这两种类型的分类法。我该如何解决这个问题? Niether get_categories 或 get_terms 似乎有一个选项可以指定你想要获取哪个 post 类型的分类法。
编辑 注意:每个 post 类型也有多个分类法
有人能帮忙吗?
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') );
$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 );
}
试试这个
<?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;
}
}
?>
我发现下面的代码很有魅力:)
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 );