Is it possible to get the ACF field name / ID where the search string matched? The ACF fields are also included with default search functionality of wordpress. So, when a search string is matched with an ACF field value, I also want to identify the field name/ID. Is that possible?
Asked
Active
Viewed 442 times
0
-
do you mean to searched text if matches with acf field then you want to get that acf field details right? – Gufran Hasan Sep 11 '18 at 14:11
-
Yes, exactly :) Sorry for my bad explanation – Imrul.H Sep 11 '18 at 14:13
2 Answers
0
You can try this code script. I hope this will work for you.
$fields = get_fields(get_the_ID());
$acfField = array();
$search_query = get_search_query();
foreach( $fields as $name => $value ):
if($search_query == $value){
$acfField['name'] = $name;
$acfField['value'] =
$value; break;
}
endforeach;
print_r($acfField);
Put this it in loop of the search.php
as :
while ( have_posts() ) : the_post();
$fields = get_fields(get_the_ID());
$acfField = array();
$search_query = get_search_query();
foreach( $fields as $name => $value ):
if($search_query == $value){
$acfField['name'] = $name;
$acfField['value'] = $value;
break;
}
endforeach;
print_r($acfField);
get_template_part( 'template-parts/post/content' );
endwhile;
Note: Actually, we can not get all acf fields. We can only get all ACF fields of particular post by post ID using get_fields(get_the_ID());

Gufran Hasan
- 8,910
- 7
- 38
- 51
0
All you need to do is add this lines to function.php
<?php
function list_searcheable_acf(){
$list_searcheable_acf = array("title", "sub_title", "excerpt_short", "excerpt_long", "xyz", "myACF");
return $list_searcheable_acf;
}
function advanced_custom_search( $where, &$wp_query ) {
global $wpdb;
if ( empty( $where ))
return $where;
// get search expression
$terms = $wp_query->query_vars[ 's' ];
// explode search expression to get search terms
$exploded = explode( ' ', $terms );
if( $exploded === FALSE || count( $exploded ) == 0 )
$exploded = array( 0 => $terms );
// reset search in order to rebuilt it as we whish
$where = '';
// get searcheable_acf, a list of advanced custom fields you want to search content in
$list_searcheable_acf = list_searcheable_acf();
foreach( $exploded as $tag ) :
$where .= "
AND (
(wp_posts.post_title LIKE '%$tag%')
OR (wp_posts.post_content LIKE '%$tag%')
OR EXISTS (
SELECT * FROM wp_postmeta
WHERE post_id = wp_posts.ID
AND (";
foreach ($list_searcheable_acf as $searcheable_acf) :
if ($searcheable_acf == $list_searcheable_acf[0]):
$where .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
else :
$where .= " OR (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
endif;
endforeach;
$where .= ")
)
OR EXISTS (
SELECT * FROM wp_comments
WHERE comment_post_ID = wp_posts.ID
AND comment_content LIKE '%$tag%'
)
OR EXISTS (
SELECT * FROM wp_terms
INNER JOIN wp_term_taxonomy
ON wp_term_taxonomy.term_id = wp_terms.term_id
INNER JOIN wp_term_relationships
ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
WHERE (
taxonomy = 'post_tag'
OR taxonomy = 'category'
OR taxonomy = 'myCustomTax'
)
AND object_id = wp_posts.ID
AND wp_terms.name LIKE '%$tag%'
)
)";
endforeach;
return $where;
}
add_filter( 'posts_search', 'advanced_custom_search', 500, 2 );

Gufran Hasan
- 8,910
- 7
- 38
- 51

Roy.N
- 79
- 1
- 1
- 7