Question regarding Askbug search box

4.49K viewsIssues
0

With the header search box I want to get search result from both questions and from the normal blog posts. How is that possible?

I am considering to buy this theme, so clearing all doubts. Thanks.

0

Hello Jayanta,

I think all you need is this:

/**
 * Add all CPT to search query.
 */
function anspress_add_cpts_to_search( $query ) {
	// Check if search page.
	if( is_search() ) {
		
		// Get post types.
		$post_types = get_post_types( [ 'public' => true, 'exclude_from_search' => false ], 'objects' );
		$searchable_cpt = array();
		
		// Add available post types.
		if( $post_types ) {
			foreach( $post_types as $type) {
				$searchable_cpt[] = $type->name;
			}
		}

		$query->set( 'post_type', $searchable_cpt );
	}

	return $query;
}
add_action( 'pre_get_posts', 'anspress_add_cpts_to_search' );

Note: this hook includes all CPT in search query. If you want only specific CPT then replace

get_post_types

with array of CPT.

commented on answer

Well, got that. Thank you so much. But what about a quora type live searchbox?

There are already lots of plugin for that purpose. Try searching for “WordPress search suggestion plugin”.

You are viewing 1 out of 1 answers, click here to view all answers.