WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Tag archive with multiple post types => breaks nav (2 posts)

  1. Bill Erickson
    Member
    Posted 3 years ago #

    I've created multiple post types (Events, Videos, Publications...) and in the register_post_type() function for each I've set 'taxonomies' => array('post_tag'). This puts the Tag taxonomy on all post types. The goal is to have tag archive pages that show content from all post types. I'm also using a custom WordPress menu with a link to a page and custom links to the post type archive pages.

    By default, WordPress only displays posts from the 'post' post type on tag archive pages. I'm running this in my functions.php file to fix it:

    /* All Posts in Tag Archives */
    add_filter('pre_get_posts', 'bcrw_tag_query');
    function bcrw_tag_query($query) {
      if(is_tag()) {
        $post_type = get_query_var('post_type');
    	if($post_type)
    	    $post_type = $post_type;
    	else
    	    $post_type = array('post','event', 'videos', 'publications', 'podcasts');
        $query->set('post_type',$post_type);
    	return $query;
        }
    }

    When this filter is disabled, the tag archive displays no results (there's no posts in the 'post' post type with that tag) and the WordPress menu works. When the filter is enabled, posts from all post types are displayed correctly, but the nav menu disappears.

    Menu working: http://dev-barnard.billerickson.net/
    Menu not working: http://dev-barnard.billerickson.net/tag/disability/

    I've also tried changing the above function from $post_type = array('post','event', 'videos', 'publications', 'podcasts'); to $post_type = 'all', thinking maybe it didn't work with arrays. I get the same result (correct posts show up, nav menu doesn't).

    1. Am I approaching this wrong? Is there a better way to make tag pages show all post types?
    2. Why is the nav menu dependent upon a single post type in query_vars?

  2. Bill Erickson
    Member
    Posted 3 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic