Home › Forums › Front-end Issues › Adding custom fields to keyword search
Hello,
I am trying to add custom fields created by ACF to my search, I am using meta_query, but my searches do not get any results from data in the ACF’s
$this_key = $_GET['keyword'];
$args = array (
'post_type' =>'resources',
'post_status' => 'publish',
'posts_per_page' => 100,
'order' => 'ASC',
's' => $this_key,
'meta_query' =>
array(
'relation' => 'OR',
array(
'key' => 'agency',
'value' => $this_key ,
'compare' => 'LIKE'
),
array(
'key' => 'available_form',
'value' => $this_key,
'compare' => 'LIKE'
)
)
);
To do this you need to https://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/
Or use a plugin, some of them are mentioned here https://support.advancedcustomfields.com/forums/topic/adding-custom-fields-search-to-wordpress-search/
If someone is looking for a solution to this problem is to recommend the plugin:
https://wordpress.org/plugins/acf-better-search/
This plugin adds to default WordPress search engine the ability to search by content from selected fields of Advanced Custom Fields plugin.
Everything works automatically, no need to add any additional code.
Just a heads up that the code linked to by John has an error. The code should only target the current query, however due to the use of the global is_search()
function, the filter will be applied to all queries on page load that happens to be a search.
The first snippet should be:
function cf_search_join( $join, $query ) {
global $wpdb;
if ( $query->is_search ) {
$join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
}
return $join;
}
add_filter('posts_join', 'cf_search_join', 10 , 2 );
Same goes forth for posts_where
and posts_distinct
.
The topic ‘Adding custom fields to keyword search’ is closed to new replies.
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.