Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
"I don't see how pre_get_posts hook is related to the custom menus" - that's because nav menu items are posts, and are queried as posts. Also: don't ever use query_posts(). Use WP_Query() instead for custom query loops, or use pre_get_posts to modify the main query. –  Chip Bennett Dec 4 '12 at 18:10
add comment

1 Answer

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;
    }
share|improve this answer
    
Didn't work - no change at all, menu is still gone/using fallback and removing is_admin condition messes up menu builder (Appearance->Menus - Pages section/widget) –  Justine May 24 '12 at 10:49
    
wordpress menus are Custom Post Type. type name is nav_menu_item. You might want to exclude that post type from $types. Though i am not sure what is you trying to do with the code. –  Sisir May 24 '12 at 11:03
    
The code is a part of a bigger search filtering plugin/code but only this part causes problems. $types definitely doesn't include nav_menu_item –  Justine May 24 '12 at 11:15
    
@Justine - try using a query var less generic than type, that's a reserved word in WordPress and may be causing a conflict. –  Milo May 24 '12 at 13:50
    
@Milo - changed the query var to myprefix_type, nothing changed :( –  Justine May 24 '12 at 14:44
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.