Site logo

Archived topic

Search results menu issue

5 replies · Started by Mike on June 30, 2020

Viewing posts 1–6 of 6

When I use the navigation search. The results page is ok, but the menu is just the magnifying glass and no menu items, so i can't go anywhere. What might be happening here?
Thanks

Hi there,

can you try disabling plugins to see if there is a conflict ?

Hi David. Had tried that - no conflict. But I put the site on a MAMP and disabled everything. Turns out it was one function in my child theme. You don't need to answer this, but I'm baffled.

This works OK

add_filter( 'pre_get_posts', 'custom_post_type_search' );
function custom_post_type_search( $query ) {
     if ($query->is_search) {
          $query->set('post_type', array( 'post', 'drlc_online', 'drlc_libraries', 'event'));
     }
     return $query;
}

But this breaks the menu:

function rc_add_cpts_to_search($query) {
	if( is_search() ) {
		$post_types = get_post_types(array('public' => true, 'exclude_from_search' => false), 'objects');
		$searchable_types = array();
		if( $post_types ) {
			foreach( $post_types as $type) {
				$searchable_types[] = $type->name;
			}
		}
		$query->set( 'post_type', $searchable_types );
	}
	return $query;
}
add_action( 'pre_get_posts', 'rc_add_cpts_to_search' );

Any idea why? As I say, not your job to answer at all, just chancing the ask :)
Mike

Hi there,

Instead of this:

if( is_search() ) {

Try this:

if ( $query->is_search() && $query->is_main_query() ) {

Thanks Tom.

You're welcome :)

This archived topic is closed to new replies.