148

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

Jesse Nickles
  • 1,435
  • 1
  • 17
  • 25
InvalidSyntax
  • 9,131
  • 20
  • 80
  • 127

9 Answers9

350

Nevermind! I found it :)

get_queried_object()->term_id;
InvalidSyntax
  • 9,131
  • 20
  • 80
  • 127
  • 37
    You could also just use `get_queried_object_id()` to retrieve just the ID. Whole snippet would be `$term_id = get_queried_object_id();` – David Apr 08 '15 at 16:27
  • Good answer . Recently i see an question in WordPress stack exchange . Could you please check . http://wordpress.stackexchange.com/questions/214453/taxonomy-subtaxonomy-child-taxonomy-of-a-product-woocommerce –  Jan 14 '16 at 10:19
  • 2
    Bear in mind this will work only in taxonomy page. But if you in taxonomy child page you won't get the taxonomy id. – Fury Jan 11 '19 at 22:14
74

Simple and easy!

get_queried_object_id()
theMukhiddin
  • 1,027
  • 8
  • 10
  • 5
    [get_queried_object_id()](https://developer.wordpress.org/reference/functions/get_queried_object_id/) was introduced in 3.1.0 (February 23, 2011) and should be the correct answer for the question asked. > `taxonomy.php` page in my wordpress template folder, I would like to get the current `term id` for a function. – Pea Dec 04 '18 at 22:21
44

Here's the whole code snippet needed:

$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
Tim Bowen
  • 587
  • 4
  • 8
23

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 .''; 
?>
Varsha Dhadge
  • 1,711
  • 14
  • 23
15

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;
Fury
  • 4,643
  • 5
  • 50
  • 80
10
<?php 
$terms = get_the_terms( $post->ID, 'taxonomy');
foreach ( $terms as $term ) {
    $termID[] = $term->term_id;
}
echo $termID[0]; 
?>
Paweł Tomkiel
  • 1,974
  • 2
  • 21
  • 39
Jadson Moreira
  • 109
  • 1
  • 4
4

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);
Purvik Dhorajiya
  • 4,662
  • 3
  • 34
  • 43
2

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 );
        }
    }
pacholik
  • 8,607
  • 9
  • 43
  • 55
0

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);
Dev
  • 457
  • 6
  • 18