How To Fetch The Current Taxonomy Term ID In WordPress

geekerhub

Updated on:

Wp_reset_query and Wp_reset_postdata

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
get_query_var(‘taxonomy’)
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

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

<?php
   $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.

3 thoughts on “How To Fetch The Current Taxonomy Term ID In WordPress”

  1. Dzięki programowi monitorowania rodziców rodzice mogą zwracać uwagę na czynności wykonywane przez ich dzieci na telefonie komórkowym oraz łatwiej i wygodniej monitorować wiadomości WhatsApp. Aplikacja działa cicho w tle urządzenia docelowego, nagrywając wiadomości konwersacyjne, emotikony, pliki multimedialne, zdjęcia i filmy. Dotyczy każdego urządzenia z systemem Android i iOS.

  2. PBN sites
    We will generate a structure of privately-owned blog network sites!

    Benefits of our privately-owned blog network:

    We carry out everything SO THAT Google DOES NOT understand THAT THIS IS A privately-owned blog network!!!

    1- We obtain domains from separate registrars

    2- The leading site is hosted on a virtual private server (Virtual Private Server is fast hosting)

    3- Additional sites are on various hostings

    4- We allocate a unique Google profile to each site with verification in Search Console.

    5- We develop websites on WordPress, we don’t use plugins with aided by which malware penetrate and through which pages on your websites are generated.

    6- We don’t duplicate templates and utilize only individual text and pictures

    We refrain from work with website design; the client, if desired, can then edit the websites to suit his wishes

Leave a Comment