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.

Ok, I'm setting up a site with a some CPT's. When creating posts under said CPT's, we added tags. When viewing the posts results after searching, tags were displayed, but upon clicking the archive page indicted that no results were available.

So, I added the code below to my functions.

function myTagFilter($query) {
$post_type = $_GET['type'];
if (is_tag()){
    if (!$post_type) {
        $post_type = 'any';
    }
    $query->set('post_type', $post_type);
}
return $query;
};
add_filter('pre_get_posts','myTagFilter');

Now, posts from the CPT's display in the tags archive, but the menu is broken. Here's a link if you want to test it out.

http://mt-spacehosting.com/fisheries/?s=framework+50

So, my question is, why would the menu break because of this?

share|improve this question
    
Which menu is broken? –  sven Jul 19 '13 at 13:06
    
The topnav and the sidebar custom menu are both not showing. –  mtuttle Jul 19 '13 at 13:18
add comment

1 Answer 1

up vote 2 down vote accepted

is_tag() is true for the entire tag archive page and for any queries that run on it. You need to restrict that code to only run for the main query on that page and that hte query is a tag query and not some other kind of query, like a menu query. You do that by checking the actual query object being used.

   function myTagFilter($query) {
      $post_type = $_GET['type'];
      if ($query->is_main_query() && $query->is_tag()){
        if (!$post_type) {
            $post_type = 'any';
        }
        $query->set('post_type', $post_type);
      }
      return $query;
    };
    add_filter('pre_get_posts','myTagFilter');

is_tag(), is_single(), and other conditionals like that are functions that check the global $wp_query object. You have to be careful not to use them to check things that are not that global object.

share|improve this answer
    
Thank you for clearing that up! –  mtuttle Jul 19 '13 at 15:46
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.