I am using this function to make my custom post type “portfolio” show up on archives/category/tag pages (in functions.php):
function namespace_add_custom_types( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array('post', 'portfolio'));
return $query;
}
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types'
The problem is that for some reason it is making my nav menu disappear. Here is the code for the nav menu (in header.php):
<?php wp_nav_menu(array( 'theme_location' => 'primary', 'sort_column' => 'menu_order', 'menu_class' => 'nav-menu', 'container_class' => 'nav-menu',) ); ?>
Any idea what I can change?
WordPress nav menus consist of posts of the
nav_menu_item
post type, and as your function changes the post type for all queries, there is nothing to display.Solution: modify only the main query by checking
is_main_query
, e.g.:PS:
pre_get_posts
is an action hook, so you should useadd_action
instead ofadd_filter
:Add this:
'nav_menu_item'
to the array in your functions.php file.Final code looks like this: