Home › Forums › General Issues › Adding custom fields search to WordPress search › Reply To: Adding custom fields search to WordPress search
it works for me
function custom_search_query( $query ) {
if ( !is_admin() && $query->is_search ) {
$result = $query->query_vars['s'];
$query->query_vars['s'] = '';
$query->set('meta_query', array('relation' => 'OR',
array(
'key' => 'acf_name', // ACF FIELD NAME OR POST META
'value' => $result,
'compare' => 'LIKE',
)
));
$query->set('post_type', 'servicios'); // optional POST TYPE
}
}
add_filter( 'pre_get_posts', 'custom_search_query');
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.