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.

In a previous question, I was struggling with how to get my custom post type(s) to show when performing standard category/tag queries, since by default Wordpress doesn't include CPT's in these queries. After quite a bit of hunting I found the answer to be this:

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if($query->is_category() || $query->is_tag() || $query->is_home() && $query->is_main_query()) {
     $post_type = get_query_var('post_type');
     if($post_type)
        $post_type = $post_type; 
     else
        $post_type = array('post','my_custom_post_type'); 
     $query->set('post_type',$post_type);
     return $query;
  }
}

And, it works great... I can now assign categories/tags to my custom post types, and then query them and see the results. HOWEVER, with this code in place, I now have an entirely new problem which ONLY happens when the above code is in place... if I now query or view my Custom Post Type directly (that is, not via a category/tag), then suddenly my standard query items on the page (such as, a category listing in the footer) go blank.

I'm not understanding how this is happening, since I am checking to assure $query->is_main_query(), so it seems the initial if statement would be false....

Do I now have to write another separate function so that when I query or view a custom post type directly, I can then allow standard post types to function on the same page?? Any help appreciated...


UPDATE

Thanks for the comments, I have figured out why this is problematic:

When I had my initial solution (not shown in this post), everything worked great with basic queries from the standard and CPT - but as I progressed into multiple queries, I realized that in certain cases, such as pages with queries spread around, in main page, some in footer, suddenly I ran into a crossover effect - like my menu would disappear at times, etc.

So that's when the && $query->is_main_query() was added (see full code above) - and of course it worked better then, because I was only applying this to the main query, thus avoiding menu/footer issues... or so I thought.

Since adding this new version of the code, all seemed well - until I queried my CPT 'DIRECTLY'... when I did this, one of my standard footer widgets (calling a simple category query) went blank.

As I started looking at this, I could see that in my initial IF query in the above code, it is performing an 'OR OR OR AND' - when technically, to keep this code to ONLY the main query, I would need to do something like '(( OR OR OR ) AND )' - or, wrap the entire function in in an if ($query->is_main_query() {

So, I went ahead and did this (see below)... and when I did it, that solved the 'Direct CPT' call problem - allowing me to now directly query a CPT without blowing up the footer query.

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
if ($query->is_main_query()) {
if($query->is_category() || $query->is_tag() || $query->is_home() ) {
$post_type = get_query_var('post_type');
if($post_type)
    $post_type = $post_type; 
else
    $post_type = array('post','my_custom_post_type'); 
$query->set('post_type',$post_type);
return $query;
    }
      }
}

So, this seems great, right?? But, not so... because since my first version of this wasn't set up as I had intended, it was allowing my CPT to be added to the standard category/tag queries at times when NOT in the main_query... so now that I have officially filtered this so it can ONLY effect the main_query in all cases, my on-page Tab query calls are failing... (these are making calls to my CPT via category within jquery tabs on each page).

So my new question is: given my SECOND version of code above, which allows me to incorporate my CPT into standard cat/tag queries, AND query my CPT directly, with no ill effect to other queries on the page - how do I now determine what query my TAB calls are making? Because it is obviously not the main_query - and therefore, I now need to target those so that I can include my CPT, much like I have done for the main query?

share|improve this question
    
I don't see any reason why you'd have this issue with this particular bit of code. a straight copy/paste into twenty twelve theme with an added custom post type works fine for me, additional queries on custom post type pages are not filtered. –  Milo Feb 19 '13 at 0:54
    
Hi, greatly appreciate your thoughts on this... please note the update I added. –  Soyo Feb 19 '13 at 15:10
    
if you're doing custom queries on your pages, you can just explicitly set the post_type in those queries, no need for the filter. –  Milo Feb 19 '13 at 15:41
    
Actually, those queries are being done by a plugin - so I plan to look into it further - I will do that and report back, thanks for your thoughts –  Soyo Feb 19 '13 at 17:54
    
Hi, yes, if I modify the plugin and make explicit call to my custom post type it is working - but surely there must be a way to allow the inclusion of my CPT for this via a function, so that standard cat and tag calls that are linked to my CPT can pull the CPT outside the main_query without modifying code - Evidently I need to learn how loops work and how to identify them so I can set up filters beyond just the main_query... –  Soyo Feb 19 '13 at 21:41
show 1 more comment

1 Answer 1

You might be looking for wp_reset_query

You would insert this after your return.

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if($query->is_category() || $query->is_tag() || $query->is_home() && $query->is_main_query()) {
 $post_type = get_query_var('post_type');
 if($post_type)
    $post_type = $post_type; 
 else
    $post_type = array('post','my_custom_post_type'); 
 $query->set('post_type',$post_type);
 return $query;
  }
wp_reset_query;
}

I really hope this helps.

share|improve this answer
    
Hello, thanks much for your thoughts on this - while that doesn't solve the issue, it seems like it should probably be a part of this - but I have found some other things out and updated the main post... –  Soyo Feb 19 '13 at 15:11
    
wp_reset_query is for resetting the global query variables after doing a custom query, it's not applicable in this case. –  Milo Feb 19 '13 at 15:39
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.