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.

Since 3.1 WordPress support multiple taxonomy query. The question is : If I have a two taxonomy query, e.g:

http://localhost:8888/site/?lot-term=australia&story-type=note

how can I determine which template to be used for the query? On the first try, it loads taxonomy-lot-term.php, but after I re install the theme, it loads taxonomy-story-type.php instead.

After some experiment, I found a way to make it use my preferred template file by using a single taxonomy query first (the preferred taxonomy template file) and altering it using query_posts. But I believe there is a better way to do it since it add another SQL query.

Thank you in advance.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

I'm not sure if this the best method, and I would like to hear some other suggestions!

This will alter the template to some pre-defined template if two or more taxonomies are being queried. You can hard-code the taxonomies to check or use get_taxonomies:

/*
* Checks to see if more than one of (certain) taxonomies are being queried
* If they are, alters the template to 'my-multitax-template.php' (if it can find it)
*/
add_filter('template_include', 'my_multietax_template');
function my_multietax_template( $template ){

    //Array of taxonomies to check
    $taxes = array('story','lot-term');

    //Or select ALL taxonomies (or pass $args array argument)
    //$taxes=array_keys(get_taxonomies('','names')); 

    //Keep track of how many of the selected taxonomies we're querying
    $count =0;
    global $wp_query;
    foreach ($taxes as $tax){
        if(isset($wp_query->query_vars[$tax] ))
            $count ++;

        if($count > 1){
            //Locate alternative template.
            $alternate = locate_template('my-multitax-template.php');
            if(!empty($alternate)
                $template = $alternate;
            break;
        }
    }
    return $template;
}

Of course 'my-multitax-template.php' could in fact be the template name of one of the taxonomies, to give that taxonomy template precedence. Or add additional, more involved logic, if you a querying more than 2 taxonomies and want WordPress to load different templates according to particular cases.

share|improve this answer
    
This looks interesting. I'll give it a try. Thank you ! –  ifdion Feb 27 '12 at 11:49
    
I can't get this filter work :(. Copy pasted the script to the bottom of function.php. Changed the $taxes = array('story-type','lot-term'); and the locate_template('taxonomy-lot-term.php'); but no luck. –  ifdion Mar 3 '12 at 6:34
1  
There is a missing right bracket after if(!empty($alternate) that might be the problem. If not, go into 'debug' mode so that you can see any error messages. –  Stephen Harris Mar 3 '12 at 12:24
    
Its not the missing bracket. I tried to remove the $template = $alternate; from the conditional statement, but nothing changed. And strangely theres no error on the page after define('WP_DEBUG', true); –  ifdion Mar 5 '12 at 3:12
1  
It should read if(!empty($alternate)). Try wp_die-ing to determine if the logic is correct (i.e. it tries to locate your alternative template) and if it is, use wp_die to see if it has located the template. –  Stephen Harris Mar 5 '12 at 11:06
show 3 more comments

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.