I’m using a custom search filter (using my custom query var type), like so:

function fteh_pre_get_posts( $query ){
    if( isset( $query->query_vars['type'] ) )
        $types = explode( ',', $query->query_vars['type'] );
        $query->set( 'post_type', $types );

    return $query;
}

if ( ! is_admin() ) {
     add_action( 'pre_get_posts', 'fteh_pre_get_posts' );
}

I noticed this causes a registered custom menu to disappear.

Without the !is_admin() it also confused the menu builder (it showed some posts instead of all my pages in Pages section). Adding the condition helped with that, but I can’t figure out how to bring back my menu.

It’s properly registered and called, build and saved in proper theme location and worked before adding that filter. Now it just uses the fallback_cb (in my case the default wp_page_menu) instead of my custom menu.

As soon as I remove that action from pre_get_posts, everyting is back to normal.

I don’t see how pre_get_posts hook is related to the custom menus – does anyone know why it would cause such a weird behavior?

UPDATE:

I now discovered another problem caused specifically by the snippet above – all my custom queries on pages (simple stuff like query_posts('post_type=organization')) stopped working and display normal posts instead of custom post types set up in query_posts.

2 s
2

Use is_main_query() to modify only the main query so menu will stay not affected.

Try This:

add_action( 'pre_get_posts', 'fteh_pre_get_posts' );

    function fteh_pre_get_posts( $query ){
        if( !is_admin() && $query->is_main_query() && isset( $query->query_vars['type'] ) )
            $types = explode( ',', $query->query_vars['type'] );
            $query->set( 'post_type', $types );

        return $query;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *