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.

I'm facing an issue with the is_tax() function.

I'm a class method that fetches a specific custom post types. These custom post types have taxonomy related custom fields (among other custom fields).

If in these custom fields I find, for instance, a value for the category term "discografia", I want my method to return this custom post type if I'm currently seeing the archive page /category/discografia.

This is an excerpt of my code:

// The Query
/** @var $query WP_Query */
$query = new WP_Query($query_args);

$results = false;
// The Loop
if ($query->have_posts()) {
    if (!is_home() && !is_front_page()) {
        $posts = $query->get_posts();
        foreach ($posts as $post) {
            $add = false;
            //ServiceBoxes::getBox returns the $post object as Array_A
            //and adds a new key "meta" with the custom fields I want to use
            $box = ServiceBoxes::getBox($post->ID);
            if (is_archive()) {
                if (isset($box['meta'][self::$prefix]['taxonomies'])) {
                    foreach ($box['meta'][self::$prefix]['taxonomies'] as $tax => $terms) {
                        if ($terms) {
                            if (is_tax($tax, $terms)) {
                                $add = true;
                            }
                        }
                    }
                }
            }
            if ($add) $results[] = $box;

        }
    } else {
        $results = $query->get_posts();
    }

    return $results;
}
return false;

Some clarification about what happens here: 1. $box['meta'][self::$prefix]['taxonomies'] actually returns an array with "category" as key and an array of terms (including "discografia") 2. The url /category/discografia/ actually returns a series of posts 3. is_tax($tax, $terms) returns false 4. Also is_tax('category', 'discografia') returns false

I suspect that the instance of WP_Query inside my method is overriding the global query, but I don't see why: it's a new instance, there is no reason for is_tax() to get confused.

However, with some step-by-step debugging here's what happens in is_tax when called as is_tax('category', array('discografia')):

  • $wp_query->is_tax( $taxonomy, $term ) is called by is_tax()
  • in $wp_query->is_tax(), if ( !$this->is_tax ) returns false

I've been able to reach this point: after that, I don't know what else I can do to understand why the is_tax property is not set as expected.

With the debugger on and some variables in the watch list, here is what I can see when calling "/category/discografia/" (an archive page that, I repeat, returns posts): http://www.evernote.com/shard/s6/sh/88f05447-edff-40c7-9670-902f0a92bc62/b50a28d39c9e55c7d404f912175975a4

share|improve this question
    
Can you show an example of the structure of $box['meta'][self::$prefix]['taxonomies'] ? It sounds like the problem might be in the foreach loop on this array, where $tax=>$terms are not assigned the values you expect. You should try debugging these values. –  birgire Jul 2 '13 at 11:39
    
I did it: $tax = "category", $terms = array('discografia') It's not a foreach issue: as I wrote, the is_tax() function called with hardcoded values returns false as well. –  Andrea Sciamanna Jul 2 '13 at 11:50
    
does is_category( array( 'discografia' ) ) work on the category page /category/discografia/ ? –  birgire Jul 2 '13 at 11:55
    
Yes, it does return true (see my last update). –  Andrea Sciamanna Jul 2 '13 at 12:02
add comment

1 Answer 1

up vote 0 down vote accepted

If you take a look at this part of the WP_Query class:

            switch ( $tax_query['taxonomy'] ) {
                case 'category':
                    $this->is_category = true;
                    break;
                case 'post_tag':
                    $this->is_tag = true;
                    break;
                default:
                    $this->is_tax = true;
            }

then it looks like on category archive pages where is_category gives true then is_tax is false.

So it looks like you will have to use a mix of is_category and is_tax ;-)

Update:

Take a look at this ticket:

is_tax() returns false when is_category() or is_tag() returns true

http://core.trac.wordpress.org/ticket/18636

It's status is currently closed with wontfix!

share|improve this answer
    
Thank you! This made things clear... well, almost: I don't quite understand why they did that, as gategories and tags are still taxonomies, but anyway, using the same switch-case I've managed to make my code work. –  Andrea Sciamanna Jul 2 '13 at 12:49
    
yes it's strange, but it looks like they have decided to live with it to keep the compatability unbroken ;-) –  birgire Jul 2 '13 at 12:59
add comment

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.