How to get the current taxonomy term ID (not the slug) in WordPress?

PhpWordpressTaxonomy

Php Problem Overview


I've created a taxonomy.php page in my WordPress theme folder. I would like to get the current term id for a function. How can I get this?

get_query_var('taxonomy') only returns the term slug, I want the ID

Php Solutions


Solution 1 - Php

Nevermind! I found it :)

get_queried_object()->term_id;

Solution 2 - Php

Simple and easy!

get_queried_object_id()

Solution 3 - Php

Here's the whole code snippet needed:

$queried_object = get_queried_object();
$term_id = $queried_object->term_id;

Solution 4 - Php

Use following code

This will print your current taxonomy name and description(optional)

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

Solution 5 - Php

If you are in taxonomy page.

That's how you get all details about the taxonomy.

get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

This is how you get the taxonomy id

$termId = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) )->term_id;

But if you are in post page (taxomony -> child)

$terms = wp_get_object_terms( get_queried_object_id(), 'taxonomy-name');
$term_id = $terms[0]->term_id;

Solution 6 - Php

<?php 
$terms = get_the_terms( $post->ID, 'taxonomy');
foreach ( $terms as $term ) {
    $termID[] = $term->term_id;
}
echo $termID[0]; 
?>

Solution 7 - Php

See wp_get_post_terms(), you'd do something like so:

global $post;
$terms = wp_get_post_terms( $post->ID, 'YOUR_TAXONOMY_NAME',array('fields' => 'ids') );

print_r($terms);

Solution 8 - Php

It's the term slug you want.Looks like you can get the id like this if that's what you need:

function get_term_link( $term, $taxonomy = '' ) {
    global $wp_rewrite;

    if ( !is_object($term) ) {
        if ( is_int( $term ) ) {
            $term = get_term( $term, $taxonomy );
        } else {
            $term = get_term_by( 'slug', $term, $taxonomy );
        }
    }

Solution 9 - Php

This works on all page types not just taxonomy term archives

$category_id = get_the_category( get_the_ID());
$id          = get_category($category_id[0]->term_id);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionInvalidSyntaxView Question on Stackoverflow
Solution 1 - PhpInvalidSyntaxView Answer on Stackoverflow
Solution 2 - PhptheMukhiddinView Answer on Stackoverflow
Solution 3 - PhpTim BowenView Answer on Stackoverflow
Solution 4 - PhpVarsha DhadgeView Answer on Stackoverflow
Solution 5 - PhpFuryView Answer on Stackoverflow
Solution 6 - PhpJadson MoreiraView Answer on Stackoverflow
Solution 7 - PhpPurvik DhorajiyaView Answer on Stackoverflow
Solution 8 - PhpSYED FARHAN KARIMView Answer on Stackoverflow
Solution 9 - PhpDevView Answer on Stackoverflow