0

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

2 Answers 2

1

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

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

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

4
  • 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. Commented Feb 24, 2012 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 Commented Feb 24, 2012 at 17:47
  • is_tax checks if its the taxonomy's archive page. Commented Feb 24, 2012 at 17:52
  • yup, just found that myself. will correct. Commented Feb 24, 2012 at 17:53

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.