Get post_type by term_id

时间:2013-06-26 作者:atwellpub

是否可以找出term\\u id属于哪个post\\u类型?

我有这段代码来尝试将所有可能的类别收集到一个数组中,但我想将每个类别所属的帖子类型添加到数组中。

$post_catgegories_objects = get_categories(
    array(
        \'type\'       => \'post\',
        \'orderby\'    => \'name\',
        \'hide_empty\' => \'0\',
    )
);

foreach ( $post_catgegories_objects as $cat ) {
    $post_catgegories[] = array(
        $cat->term_id,
        $cat->name,
        $cat->slug,
    );
}

// var_dump($post_type);
// exit;

$taxonomies = get_taxonomies(
    array(
        \'public\'   => true,
        \'_builtin\' => false,
    )
);

foreach ( $taxonomies as $key => $taxonomy ) {
    if ( stristr( $taxonomy, \'cat\' ) ) {
        $tax_terms = get_terms( $taxonomy );

        // print_r($tax_terms);
        // exit;

        foreach ( $tax_terms as $term ) {
            $cpt_categegories[] = array(
                $term->term_id,
                $term->name,
                $term->slug,
            );
        }
    }
}

$all_categories = array_merge( $post_catgegories, $cpt_categegories );

print_r( $all_categories );
exit;

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

如果这是您的意思,您可以获取术语分类所附加的帖子类型。

$post_type = get_taxonomy( $term->taxonomy )->object_type[0];

SO网友:ksr89

你可以得到post_type 通过term_id

$args = array(\'hide_empty\' => 0,\'orderby\' => \'name\');
$categories = get_categories($args);
foreach ($categories as $key => $value) {
    $term = get_term_by(\'id\', $value->term_id , \'activity_category\');
    $post_type = get_taxonomy( $term->taxonomy )->object_type[0];
}

结束

相关推荐