$wp_query->queried_object->ID throws warning: Undefined property

    $current_term = $wp_query->queried_object->name;
    $current_term_id = $wp_query->queried_object->ID;

<hgroup class="section-heading wrapper">
    <h1><?php echo $current_term; ?></h1>
    <h3><?php echo category_description( $current_term_id ); ?></h3>

This works fine but the $current_term_id variable throws a warning …

Notice: Undefined property: stdClass::$ID in /Users/my/htdocs/wr/wp-content/themes/wr/taxonomy-event_type.php on line 12

Any ideas what I’m doing wrong here or what’s the real property for the ID of the current term?

Solutions Collecting From Web of "$wp_query->queried_object->ID throws warning: Undefined property"

I faced the exact same problem for days and then found this!
Use the function $wp_query->get_queried_object_id(). Check this for more details.

There’s also a wrapper for this function get_queried_object_id(). It calls all parts like global $wp_query by itself.


Always use the most low level API, until you’re forced to do otherwise.

The definition of API by the (current) wikipedia article is like the following

An application programming interface (API) is a specification intended to be used as an interface by software components to communicate with each other.

This means, that it was written to communicate with core internals and your theme or plugin.


Because of one easy reason: A wrapping API allows cores internals to transform, while still staying compatible with whatever you throw in as arguments. A wrapper function can add backwards compatibility where a core class/object may not.

It also shortens your code, makes it more readable and allows you to focus on what needs to be done by your code.

When you are on a taxonomy archive (is_tax() is true) queried_object is populated in this way (example from my site):

public 'queried_object' =>
public 'term_id' => int 2
public 'name' => string 'Puglia' (length=6)
public 'slug' => string 'puglia' (length=6)
public 'term_group' => int 0
public 'term_taxonomy_id' => int 7
public 'taxonomy' => string 'region' (length=6)
public 'description' => string 'Bari, Brindisi, Lecce, Taranto, Foggia' (length=38)
public 'parent' => int 0
public 'count' => int 1
public 'filter' => string 'raw' (length=3)
public 'queried_object_id' => int 2

So you directly get both name and description. The code you can use is:

    $current_term_name = $wp_query->queried_object->name;
    $current_term_description = $wp_query->queried_object->description;

<hgroup class="section-heading wrapper">
    <h1><?php echo $current_term_name; ?></h1>
    <h3><?php echo $current_term_description; ?></h3>

$wp_query->queried_object->ID has several potential points of failure:

  • Your query may not be dealing with a WP_Post object. The queried_object can also reference a taxonomy in some circumstances.
  • Your query may not have actually identified any results. Your queried_object could be null, and thus you’re asking for a property on an nonexistent object.
  • You could be using $wp_query in code that does not have a reference to that global. If you determine that $wp_query itself is null, you should add

    global $wp_query; 

    … somewhere above your code.