Function to display custom post type on front page makes menu items dissapear

Here’s a breakdown of what’s happening with the code you’ve posted.

This code is saying, If this is the home page and the main query is being used, don’t do anything.

if ( is_home() && $query->is_main_query() )

return;

That’s no good! The intention is to modify the query if we’re on the homepage and the main query is being run.

Then we have:

$query->set( 'post_type', array( 'post', 'reviews' ) );

This code is correctly setting the query up to get posts of the post types post and reviews, but because of the conditional statement above it, it’s going to be firing for non main queries, which is why it’s messing up your menus. Navigation menus and secondary loops don’t use the main query.

Here’s an updated version of your code that will work:

add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
    // Bail if this is not the home page or if it's not the main query.
    if ( ! is_home() && ! $query->is_main_query() ) {
        return;
    }

    $query->set( 'post_type', array( 'post', 'reviews' ) );
}

tech