Main menu not appearing on category pages

I know this question is quite old but since there is no answer yet an there are many similar questions on the WordPress support forums, I’ll better share my findings and maybe help someone.

The issue of disappearing menu may be caused by code in plugin or theme incorrectly modifying global $wp_query object using pre_get_posts filter hook.

I discovered that the code causing issues was in theme:

function namespace_add_custom_types( $query ) {
  if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {

    $query->set( 'post_type', array(
                'post',
                'projects',
            ));
        return $query;
    }
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );

This snippet is supposed to add custom post types to category archives but it also affected query in wp_nav_menu.

To fix the issue, I had to correct the if condition:

function namespace_add_custom_types( $query ) {
  if( is_archive() && (is_category() || is_tag()) && empty( $query->query_vars['suppress_filters'] ) ) {

    $query->set( 'post_type', array(
                'post',
                'projects',
            ));
        return $query;
    }
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );

YMMV and the cause could be entirely different, but this is how I fixed the issue of missing menu in category template.

Leave a Comment