Skip Navigation

[Resolved] Include custom fields in search results

This support ticket is created 8 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 1 reply, has 1 voice.

Last updated by Bonnie 8 years, 3 months ago.

Author
Posts
#371748

I've got a number of custom post types on my website and want to include them within the main site search. I've managed to get this working with the following code in functions.php:

// Include all post types in search
// =============================================================================
function include_post_types_in_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', 'include_post_types_in_search');

The only problem is, this code searches only the titles and content of the custom post types, not the custom fields. Does anyone know of anything I can add to this code to make sure it includes all custom fields as well?

#371753

Continued searching and managed to find a solution online:

hidden link

Just in case anyone else has the same issue!

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.