wp_nav_menu() not working on Custom Search Page

I’m using WP Custom Search plugin to generate Advanced Search in one of my newly developed WP site. Though the plugin is not completely bug-free, but it’s working just fine (excepts some Undefined Offset warnings, I’m ignoring them for its better support in searching).

But recently noticed that the wp_nav_menu() on the header.php is not working when I’m on the search page. It’s a simple code as it’s used in many of my WP sites:

<?php wp_nav_menu( array( 'theme_location' => 'header_menu', 'menu_class' => 'site-header-menu' ) ); ?>

That’s working on all other pages, BUT the search.php – a typical search template.

I’ve tried the most-cited solution from:

  • this WP Support thread — failed,
  • this SO Thread — failed, and
  • this WPSE thread — failed

Typically the search page contains a ?s= on the URL, but using this plugin, I’m getting:

http://example.com/?search-class=DB_CustomSearch_Widget-db_customsearch_widget&widget_number=preset-1&[search_queries_and_conditions]=&search=Search

Is that a cause I’m failing echoing the menu? (Live site here, and the Advanced Search is on the left)

2 Answers
2

I know this is an old thread but the problem still exists. I think this is a bug. Default queries such as nav should not be affected in making custom search page. Anyways, this is how I fixed it:

function fix_nav_menu_in_search($query)
{
    if (is_search()) {
        $query->set('post_type', ['your_cpt', 'nav_menu_item']);
    }

    return $query;
}
add_filter('pre_get_posts', 'fix_nav_menu_in_search');

Leave a Comment