获取自定义帖子类型使用的分类术语

时间:2013-10-14 作者:Grant Palin

我有一个自定义的帖子类型-case - 我正在为其构建一个归档页面。此类型有三种自定义分类法-micro_organisms, infection_types, 和antibiotic_types. 这些分类法是分级的,有各种各样的术语,最多有三个层次。对于给定的帖子类型,我想列出一些术语,比如antibiotic_types 分类法case 条目。

而在antibiotic_types 任何case 条目,我想显示指向所选术语的层次结构。

到目前为止,我已经完成了以下工作,这将导致一个只包含选定术语的平面列表,而不反映术语所处的层次结构。

// sourced from:
// http://wordpress.stackexchange.com/questions/66015/how-to-get-a-list-of-taxonomy-terms-which-are-being-used-only-within-certain-pos
function get_terms_by_post_type( $taxonomies, $post_types ) {
    global $wpdb;
    $query = $wpdb->get_results( "SELECT t.*, COUNT(*) AS count from $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id WHERE p.post_type IN(\'" . join( "\', \'", $post_types ) . "\') AND tt.taxonomy IN(\'" . join( "\', \'", $taxonomies ) . "\') GROUP BY t.term_id");
    return $query;
}
?>
...
<ul class="taxonomy-term-list">
<?php
$terms = get_terms_by_post_type(array($tax), array(\'case\'));

foreach ($terms as $term):
    $termData = get_term_by(\'id\', $term->term_id, $tax);
    $termName = $termData->name;
    $termUrl = get_term_link($termData);
?>
    <li class="taxonomy-term"><a href="<?php echo $termUrl; ?>?post_type=<?php echo $type; ?>"><?php echo $termName; ?> (<?php echo $term->count; ?>)</a></li>
<?php
endforeach;
?>
</ul>
输出类似于:

当所需输出为:

β-内酰胺类、头孢菌素类、头孢曲松类、青霉素类、青霉素g类、抗真菌类、两性霉素b类、5-氟胞嘧啶类等,我也尝试使用以下药物:

wp_list_categories(
    array(
        \'taxonomy\' => \'antibiotic_types\',
        \'hierarchical\' => 1,
        \'hide_empty\' => 1,
        \'title_li\' => \'\'
    )
);
它显示了给定分类法的所有术语,甚至那些没有被任何case 条目。

浏览了函数引用后,它看起来并没有任何内置函数来完成我想要实现的功能,无论如何都不是直接实现的。到目前为止,我还没有对分类查询做太多的工作。有什么建议吗?

2 个回复
最合适的回答,由SO网友:gmazzap 整理而成

如果你看到wp_list_categories 在codex中,您将看到它接受include param,它应该包含一个以逗号分隔的类别ID列表。

所以如果你改变你的功能get_terms_by_post_type 创建函数get_terms_id_by_post_type 仅检索术语ID,如下所示:

function get_terms_id_by_post_type( $taxonomies, $post_types ) {
    global $wpdb;
    $query = $wpdb->get_col( "SELECT t.term_id from $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id WHERE p.post_type IN(\'" . join( "\', \'", $post_types ) . "\') AND tt.taxonomy IN(\'" . join( "\', \'", $taxonomies ) . "\') GROUP BY t.term_id");
    return $query;
}
然后您可以:

// retrieve the term ids used by post type
$terms = get_terms_id_by_post_type( array($tax), array(\'case\') );

// if that terms exist implode in a commasepared string
$term_list = ! empty($terms) ? implode(\',\', $terms) : false;

// use wp_list_categories with include param to included only used terms
if ( $term_list ) {
  wp_list_categories(
    array(
        \'taxonomy\' => \'antibiotic_types\',
        \'hierarchical\' => 1,
        \'hide_empty\' => 1,
        \'title_li\' => \'\',
        \'include\' => $term_list
    )
  );
}

SO网友:natebeaty

为了防止其他人在google上搜索时只需要显示CPT帖子使用的一种分类法中的术语到另一种分类法,以下是我使用的代码,基于@gmazzap的回答:

// Get all story post IDs set to story_type of "rn"
$story_post_ids = get_posts([
  \'numberposts\' => -1,
  \'fields\'      => \'ids\',
  \'post_type\'   => \'story\',
  \'tax_query\'   => [
    [
      \'taxonomy\' => \'story_type\',
      \'field\'    => \'slug\',
      \'terms\'    => [\'rn\'],
      \'compare\'  => \'IN\',
    ]
  ],
]);

// Find story topics in use by those post IDs
$story_topic_ids = $wpdb->get_col("
      SELECT t.term_id FROM $wpdb->terms AS t
      INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
      INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id
      WHERE tt.taxonomy IN(\'story_topic\')
      AND r.object_id IN (".implode(\',\', $story_post_ids).")
      GROUP BY t.term_id
");

// Pull those topics
$story_topics = get_terms([
  \'taxonomy\' => \'story_topic\',
  \'include\'  => $story_topic_ids,
]);
这将查找类型的所有术语story_topic 由设置为术语的职位使用的story_type “rn”的

结束

相关推荐

rewrite rules hierarchical

我希望我的WordPress遵循以下规则:/productes/ 包含所有产品的页面/productes/[category]/ 包含该类别所有产品的分类页面/productes/[category]/[subcategory]/ 包含该类别所有产品(与2相同)的分类页面/[productes]/[category]/[product]/ A.single-productes.php 将显示类别产品/[productes]/[category]/[subcategory]/[product]/ A.sin

获取自定义帖子类型使用的分类术语 - 小码农CODE - 行之有效找到问题解决它

获取自定义帖子类型使用的分类术语

时间:2013-10-14 作者:Grant Palin

我有一个自定义的帖子类型-case - 我正在为其构建一个归档页面。此类型有三种自定义分类法-micro_organisms, infection_types, 和antibiotic_types. 这些分类法是分级的,有各种各样的术语,最多有三个层次。对于给定的帖子类型,我想列出一些术语,比如antibiotic_types 分类法case 条目。

而在antibiotic_types 任何case 条目,我想显示指向所选术语的层次结构。

到目前为止,我已经完成了以下工作,这将导致一个只包含选定术语的平面列表,而不反映术语所处的层次结构。

// sourced from:
// http://wordpress.stackexchange.com/questions/66015/how-to-get-a-list-of-taxonomy-terms-which-are-being-used-only-within-certain-pos
function get_terms_by_post_type( $taxonomies, $post_types ) {
    global $wpdb;
    $query = $wpdb->get_results( "SELECT t.*, COUNT(*) AS count from $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id WHERE p.post_type IN(\'" . join( "\', \'", $post_types ) . "\') AND tt.taxonomy IN(\'" . join( "\', \'", $taxonomies ) . "\') GROUP BY t.term_id");
    return $query;
}
?>
...
<ul class="taxonomy-term-list">
<?php
$terms = get_terms_by_post_type(array($tax), array(\'case\'));

foreach ($terms as $term):
    $termData = get_term_by(\'id\', $term->term_id, $tax);
    $termName = $termData->name;
    $termUrl = get_term_link($termData);
?>
    <li class="taxonomy-term"><a href="<?php echo $termUrl; ?>?post_type=<?php echo $type; ?>"><?php echo $termName; ?> (<?php echo $term->count; ?>)</a></li>
<?php
endforeach;
?>
</ul>
输出类似于:

当所需输出为:

β-内酰胺类、头孢菌素类、头孢曲松类、青霉素类、青霉素g类、抗真菌类、两性霉素b类、5-氟胞嘧啶类等,我也尝试使用以下药物:

wp_list_categories(
    array(
        \'taxonomy\' => \'antibiotic_types\',
        \'hierarchical\' => 1,
        \'hide_empty\' => 1,
        \'title_li\' => \'\'
    )
);
它显示了给定分类法的所有术语,甚至那些没有被任何case 条目。

浏览了函数引用后,它看起来并没有任何内置函数来完成我想要实现的功能,无论如何都不是直接实现的。到目前为止,我还没有对分类查询做太多的工作。有什么建议吗?

2 个回复
最合适的回答,由SO网友:gmazzap 整理而成

如果你看到wp_list_categories 在codex中,您将看到它接受include param,它应该包含一个以逗号分隔的类别ID列表。

所以如果你改变你的功能get_terms_by_post_type 创建函数get_terms_id_by_post_type 仅检索术语ID,如下所示:

function get_terms_id_by_post_type( $taxonomies, $post_types ) {
    global $wpdb;
    $query = $wpdb->get_col( "SELECT t.term_id from $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id WHERE p.post_type IN(\'" . join( "\', \'", $post_types ) . "\') AND tt.taxonomy IN(\'" . join( "\', \'", $taxonomies ) . "\') GROUP BY t.term_id");
    return $query;
}
然后您可以:

// retrieve the term ids used by post type
$terms = get_terms_id_by_post_type( array($tax), array(\'case\') );

// if that terms exist implode in a commasepared string
$term_list = ! empty($terms) ? implode(\',\', $terms) : false;

// use wp_list_categories with include param to included only used terms
if ( $term_list ) {
  wp_list_categories(
    array(
        \'taxonomy\' => \'antibiotic_types\',
        \'hierarchical\' => 1,
        \'hide_empty\' => 1,
        \'title_li\' => \'\',
        \'include\' => $term_list
    )
  );
}

SO网友:natebeaty

为了防止其他人在google上搜索时只需要显示CPT帖子使用的一种分类法中的术语到另一种分类法,以下是我使用的代码,基于@gmazzap的回答:

// Get all story post IDs set to story_type of "rn"
$story_post_ids = get_posts([
  \'numberposts\' => -1,
  \'fields\'      => \'ids\',
  \'post_type\'   => \'story\',
  \'tax_query\'   => [
    [
      \'taxonomy\' => \'story_type\',
      \'field\'    => \'slug\',
      \'terms\'    => [\'rn\'],
      \'compare\'  => \'IN\',
    ]
  ],
]);

// Find story topics in use by those post IDs
$story_topic_ids = $wpdb->get_col("
      SELECT t.term_id FROM $wpdb->terms AS t
      INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
      INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id
      WHERE tt.taxonomy IN(\'story_topic\')
      AND r.object_id IN (".implode(\',\', $story_post_ids).")
      GROUP BY t.term_id
");

// Pull those topics
$story_topics = get_terms([
  \'taxonomy\' => \'story_topic\',
  \'include\'  => $story_topic_ids,
]);
这将查找类型的所有术语story_topic 由设置为术语的职位使用的story_type “rn”的

相关推荐

rewrite rules hierarchical

我希望我的WordPress遵循以下规则:/productes/ 包含所有产品的页面/productes/[category]/ 包含该类别所有产品的分类页面/productes/[category]/[subcategory]/ 包含该类别所有产品(与2相同)的分类页面/[productes]/[category]/[product]/ A.single-productes.php 将显示类别产品/[productes]/[category]/[subcategory]/[product]/ A.sin