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 badges1 Answer
Reset to default 3You 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.