Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Ok, I have an archive loop.

In this loop I have this...

<?php if ( has_term('race') ) { ?>
   <strong class="archive-event"><?php the_title($event); ?></strong>
<?php } ?>

But it does no show whats anything!?

And I have these terms assigned to the post...

  • Tax: report-type Term: race
  • Tax: season-year Term: motogp-2013

My question is why does this not return nothing? Or what function should I be using to display something (in the loop) if the post has the term assigned to it?

Weirdly this works...

<?php if ( has_term('race', 'report-type', $post->ID )) { ?>
share|improve this question

1 Answer 1

up vote 1 down vote accepted

I suspect that the Codex information for has_term() is incorrect:

<?php has_term( $term, $taxonomy, $post ) ?>

And for the $taxonomy parameter:

$taxonomy (string) (optional) Taxonomy name Default: ''

But if you look at the source for has_term():

$r = is_object_in_term( $post->ID, $taxonomy, $term );

So, $taxonomy is passed to is_object_in_term():

<?php is_object_in_term( $object_id, $taxonomy, $terms = null ) ?>

Which says for the $taxonomy parameter:

$taxonomy (string) (required) A single taxonomy name. Default: None

(A look at source for is_object_in_term() confirms that the $taxonomy parameter is required, and will result in returning false if not passed.)

So, it would appear that failing to pass $taxonomy to has_term() will result in has_term() returning false.

It appears that the source inline docs have been updated accordingly:

* @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for.
* @param string $taxonomy Taxonomy name
* @param int|object $post Optional. Post to check instead of the current post.
* @return bool True if the current post has any of the given tags (or any tag, if no tag specified).

The $taxonomy parameter is no longer listed as Optional.

share|improve this answer
    
Note: I've updated the Codex entry for has_term() accordingly. –  Chip Bennett Jan 7 at 18:03
    
Thanks chip for such useful insight on this. +1 –  Joshc Jan 8 at 0:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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