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?
Continued searching and managed to find a solution online:
hidden link
Just in case anyone else has the same issue!