How To Fetch The Current Taxonomy Term ID In WordPress

Photo of author
Written By geekerhub

Experienced WordPress developer with over 10 years of experience working with the platform

WordPress is one of the most used platforms to develop a website. With its easy-to-use features, a non-technical one can create a website. Though, WordPress is not just limited to it. It has more than that.In WordPress, you may need to fetch the current taxonomy term ID for various purposes, such as displaying specific content or customizing a template based on the current term. This can be done using a simple function.

WordPress is like an iceberg, it is more in the backend than the front end. So, let’s come to the point where most WordPress developers feel stuck.

How to Fetch The Current Taxonomy Term Id (Not The Slug)

When you create a taxonomy.php page in a theme folder and need to get the current ID, you mostly use
Well, it returns only a term slug, not the term ID. This is where WordPress guys are stuck.
To fix it, we have multiple options. Let’s see them one by one.

Use the below codes to get the term ID

this was included in WordPress 3.1.0 version (23 Feb 2011).
Print your current name and Desc of the taxonomy.

   $tax = $wp_query->get_queried_object();
   echo ''. $tax->name . '';
   echo "<br>";
   echo ''. $tax->description .'';

Here is the another way to get term ID
$obj = get_queried_object();
echo $obj->term_id;
Now, let’s go in deep to understand some terms and functions.

Show Taxonomy Terms

$args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
$terms = wp_get_post_terms( $post_id, $taxonomy, $args );

Taxonomy From Post ID

$terms = get_the_terms( $post->ID, 'your-taxonomy' );
if ( ! empty( $terms ) ){
       $term = array_shift( $terms );
       echo $term->slug;

Hope your issue is now resolved and you have got the Taxonomy term ID, not the slug. If you still have an issue, raise your issue in the comment, we would love to help you fix the issue and make your website run smoothly.

Leave a Comment