最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get Custom Taxonomy ID from the post ID

programmeradmin2浏览0评论

In my setup, I have two custom taxonomy called Regions & Sections. All the post in site can either be from region taxonomy or from section taxonomy.

At certain point outside the loop I have the post details and I need the taxonomy ID. I don't have the taxonomy name at that point to use get_the_terms function.

Any help will be very appreciated.

In my setup, I have two custom taxonomy called Regions & Sections. All the post in site can either be from region taxonomy or from section taxonomy.

At certain point outside the loop I have the post details and I need the taxonomy ID. I don't have the taxonomy name at that point to use get_the_terms function.

Any help will be very appreciated.

Share Improve this question asked Jun 22, 2017 at 16:46 SD433SD433 631 silver badge10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

You can use wp_get_post_terms() which will return all the terms attached to the post, if any. Then, the first term of the array will be able to tell you which taxonomy it belongs to:

global $post;

$terms = wp_get_post_terms( $post->ID, array( 'regions', 'sections' ) );

if( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
    $taxonomy = $terms[0]->taxonomy;
}

I do not know what your taxonomy slugs are so I guess, you may need to change them to fit your specific setup.

发布评论

评论列表(0)

  1. 暂无评论