Adding custom fields to WordPress search
-
I have search quite a lot for this fix but for the life of me can’t find it and wondered if anyone here could help?
I am using Advanced Custom Fields 4.4.2 to store a list of business types for a particular Post (Customer). For each post a custom field called “business type” will have a number of values that can be selected i.e. Dr’s, Surgery, Doctors, First Aid etc.
I would like to be able to include the “business-type” custom field in the search so that all posts (customers) that have this business type will show, alongside any post with that text in is also (which may not contain the custom field in it).
I have tried using a function but I guess this turns the query into an “AND” statement rather than an “OR” (Join) statement.
add_action(‘pre_get_posts’, ‘modify_post_search’);
function modify_post_search($query)
{
if($query->is_search)
{
$query->set(‘meta_key’, ‘business_type’);
$query->set(‘meta_value’, get_search_query());
}
}add_filter(‘pre_get_posts’, ‘filter_search’);
;Any help would be appreciated.
- The topic ‘Adding custom fields to WordPress search’ is closed to new replies.