5
<?php 
    $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>
</hgroup>

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?

4 Answers 4

9

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.

4

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

API

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.

Why?

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.

2

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' =>
object(stdClass)[163]
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:

<?php 
    $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>
</hgroup>
3
  • 3
    Is this a guess or a definitive solution? If the latter, you might want to wrap it in a more detailed description (maybe provide a link to the relevant part of the WP_Query source or to its documentation. If the earlier, it would be a better fit for a comment, rather than an answer. Sep 26, 2014 at 15:07
  • This is a solution, I edited it to better explain the code. This was my first post. Sep 26, 2014 at 18:53
  • 1
    +1, this became a quality answer. Note that "Try this" is almost never a good way to introduce a solution. Now, with context I can most certainly upvote this. Sep 27, 2014 at 9:27
1

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

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.