0

I am trying to get the term_id of the current category archive. On the category archive, when I var_dump($wp_query)

object(WP_Query)#1189 (55) {
  ["query"]=>
  NULL
  ["query_vars"]=>
  array(0) {
  }
  ["tax_query"]=>
  NULL
  ["meta_query"]=>
  bool(false)
  ["date_query"]=>
  bool(false)
  ["queried_object"]=>
  NULL
  ["queried_object_id"]=>
  NULL
  ["request"]=>
  NULL
  ["posts"]=>
  NULL
  ["post_count"]=>
  int(0)
  ["current_post"]=>
  int(-1)
  ["in_the_loop"]=>
  bool(false)
  ["post"]=>
  NULL
  ["comments"]=>
  NULL
  ["comment_count"]=>
  int(0)
  ["current_comment"]=>
  int(-1)
  ["comment"]=>
  NULL
  ["found_posts"]=>
  int(0)
  ["max_num_pages"]=>
  int(0)
  ["max_num_comment_pages"]=>
  int(0)
  ["is_single"]=>
  bool(false)
  ["is_preview"]=>
  bool(false)
  ["is_page"]=>
  bool(false)
  ["is_archive"]=>
  bool(false)
  ["is_date"]=>
  bool(false)
  ["is_year"]=>
  bool(false)
  ["is_month"]=>
  bool(false)
  ["is_day"]=>
  bool(false)
  ["is_time"]=>
  bool(false)
  ["is_author"]=>
  bool(false)
  ["is_category"]=>
  bool(false)
  ["is_tag"]=>
  bool(false)
  ["is_tax"]=>
  bool(false)
  ["is_search"]=>
  bool(false)
  ["is_feed"]=>
  bool(false)
  ["is_comment_feed"]=>
  bool(false)
  ["is_trackback"]=>
  bool(false)
  ["is_home"]=>
  bool(false)
  ["is_privacy_policy"]=>
  bool(false)
  ["is_404"]=>
  bool(false)
  ["is_embed"]=>
  bool(false)
  ["is_paged"]=>
  bool(false)
  ["is_admin"]=>
  bool(false)
  ["is_attachment"]=>
  bool(false)
  ["is_singular"]=>
  bool(false)
  ["is_robots"]=>
  bool(false)
  ["is_favicon"]=>
  bool(false)
  ["is_posts_page"]=>
  bool(false)
  ["is_post_type_archive"]=>
  bool(false)
  ["query_vars_hash":"WP_Query":private]=>
  bool(false)
  ["query_vars_changed":"WP_Query":private]=>
  bool(true)
  ["thumbnails_cached"]=>
  bool(false)
  ["stopwords":"WP_Query":private]=>
  NULL
  ["compat_fields":"WP_Query":private]=>
  array(2) {
    [0]=>
    string(15) "query_vars_hash"
    [1]=>
    string(18) "query_vars_changed"
  }
  ["compat_methods":"WP_Query":private]=>
  array(2) {
    [0]=>
    string(16) "init_query_flags"
    [1]=>
    string(15) "parse_tax_query"
  }
}

I have a custom post type item that uses the category taxonomy.

In the admin bar, I get Edit Category and it loads the correct link with the term ID like so: /wp-admin/term.php?taxonomy=category&tag_ID=22&post_type=post

Thanks!

3
  • Try: global $wp_query; $the_category = $wp_query->get_queried_object(); var_dump( $the_category ); that should get you the info you need at the category/archive template level, but not within the loops of those templates.
    – admcfajn
    Jul 12, 2022 at 4:06
  • You can just use get_queried_object() on its own. Jul 12, 2022 at 7:47
  • This appears to be an issue with Elementor, which is where I built the category template. I was updating the query filter, which is why I needed the term id. When I test with category.php, the wp_query object works.
    – Alexnl
    Jul 12, 2022 at 14:51

1 Answer 1

0

To get term_id of current taxonomy, use the following code snippet

$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
1
  • Thanks! This works well on the category.php and when added to the query filter: ` `` add_action( 'powerpack/query/item-cat', function( $query ) { $queried_object = get_queried_object(); $query_vars = array( 'post_type' => 'item', 'post_status' => 'publish', 'cat' => $queried_object->term_id, 'posts_per_page' => 150, 'orderby' => 'title', 'order' => 'ASC' ); foreach($query_vars as $key => $value) { $query->set( $key, $value ); } } ); ```
    – Alexnl
    Jul 12, 2022 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.