6

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.

1
  • "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. Commented Dec 4, 2012 at 18:10

2 Answers 2

13

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;
    }
9
  • 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
    Commented May 24, 2012 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
    Commented May 24, 2012 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
    Commented May 24, 2012 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
    Commented May 24, 2012 at 13:50
  • @Milo - changed the query var to myprefix_type, nothing changed :(
    – Justine
    Commented May 24, 2012 at 14:44
0

I might be a little late to the party but this is what I did to fix my issue with the menu disappearing when I was viewing custom posts by category. I am using default post categories and not custom taxonomies.

public function include_custom_post_search( $query ) {
        if( ! is_admin() ) {

            if( is_category() && 'nav_menu_item' !== $query->get('post_type') )
                $query->set( 'post_type', 'custom_post_place_slug' );
        }
    }

    add_action( 'pre_get_posts', 'include_custom_post_search' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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