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 have two cpt's, events and reports, they both share the taxonomy locations, I have my taxonomy-locations.php, but that is showing reports & events for location, I just want to show reports for location. Can someone help me out with how this query should go?

Thanks

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Before the loop in your taxonomy-locations.php file,

global $wp_query;
$args = array_merge( $wp_query->query, array( 'post_type' => 'report' ) );
query_posts( $args );

You could, alternatively, modify the query using an appropriate hook (this would be the most effecient method) - but since it is run for most queries, you would need to check it's actually a query for which you want to set post type to 'report'. For example:

function my_restrict_to_report() {
    //And any other checks
    if (!is_admin() && is_tax('location')) {
        set_query_var('post_type','report');
    }
}
add_action( 'pre_get_posts', 'my_restrict_to_report' );

Not tested

share|improve this answer
    
Thanks Stephen, perfect timing as I was just asked to show the site in its current status. –  Chuck Feb 24 '12 at 17:56
    
nice job remembering the is_admin() check –  helgatheviking Feb 24 '12 at 18:01
    
@helgatheviking I'm not sure it's necessary - I think is_tax covers it, but better safe than sorry I suppose... –  Stephen Harris Feb 24 '12 at 18:05
add comment

i believe you can also do this via the pre_get_posts action. the $query variable is the entire query object and it is passed 'by reference' meaning that anything you do to it inside the function will modify the original query

add_action('pre_get_posts', 'kia_limit_tax' );

function kia_limit_tax( $query ) {
    //check that we're in the locations taxonomy before modifying the query
    if(is_tax('locations')){
        set_query_var('post_type','event');
            //which is the more elegant way to write this:
            //$query->set('post_type','event');
    }
}

is_taxonomy() is deprecated and is_tax() should be used http://codex.wordpress.org/Function_Reference/is_tax

share|improve this answer
    
Good point about the set_query_var. is_taxonomy is depreciated though, and returns true if 'locations' exists as a taxonomy, not if the locations archive page is being displayed. –  Stephen Harris Feb 24 '12 at 17:26
    
really? that's a bummer. how are you supposed to do it now then? there is clearly a difference between whether a tax exists and whether you are on that tax's archive.. to taxonomy_exists() doesn't seem right –  helgatheviking Feb 24 '12 at 17:47
    
is_tax checks if its the taxonomy's archive page. –  Stephen Harris Feb 24 '12 at 17:52
    
yup, just found that myself. will correct. –  helgatheviking Feb 24 '12 at 17:53
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.