I'm trying to organize my search results page based on an ACF subfield value of a repeater field. I would like a LIKE result rather than =.
So far, I got the following (modified with help below, but still not working):
// Modify meta key to allow wildcard
function add_wildcard_to_meta_key_filter( $where ) {
$where = str_replace("meta_key = 'test_repeater_%", "meta_key LIKE 'test_repeater_%", $where);
return $where;
}
add_filter('posts_where', 'add_wildcard_to_meta_key_filter');
//Modify search query
function alter_search_query($query) {
if ( !$query->is_search )
return $query;
$search = $query->query;
$query->set('post_type' ,'page');
$query->set('meta_query', array(
array(
'meta_key' => 'test_repeater_%_test_sub_field',
'meta_value' => '%'.$search.'%',
'compare' => 'LIKE',
),
));
$query->set('s', '');
}
add_action('pre_get_posts','alter_search_query');
It works fine if I add the exact value, i.e. "beef tenderloin", but if I enter "beef" it fails. How do I get the search to be more generic?
I installed the Query Monitor plugin and noticed I got the following SQL
SELECT SQL_CALC_FOUND_ROWS wpfb_posts.ID
FROM wpfb_posts
INNER JOIN wpfb_postmeta
ON ( wpfb_posts.ID = wpfb_postmeta.post_id )
WHERE 1=1
AND ( ( wpfb_postmeta.meta_key LIKE 'test_repeater_%_test_sub_field'
AND wpfb_postmeta.meta_value IN ('beef') ) )
AND wpfb_posts.post_type = 'page'
AND (wpfb_posts.post_status = 'publish'
OR wpfb_posts.post_status = 'acf-disabled'
OR wpfb_posts.post_status = 'private')
GROUP BY wpfb_posts.ID
ORDER BY wpfb_posts.post_date DESC
LIMIT 0, 10
If I change this:
AND ( ( wpfb_postmeta.meta_key LIKE 'test_repeater_%_test_sub_field'
AND wpfb_postmeta.meta_value IN ('beef') ) )
To this:
AND ( ( wpfb_postmeta.meta_key LIKE 'test_repeater_%_test_sub_field'
AND wpfb_postmeta.meta_value LIKE '%beef%' ) )
And run that in phpMyAdmin's SQL, I get the desired row. The question is, how can I change the query to look like that using WordPress functions?
I'm trying to organize my search results page based on an ACF subfield value of a repeater field. I would like a LIKE result rather than =.
So far, I got the following (modified with help below, but still not working):
// Modify meta key to allow wildcard
function add_wildcard_to_meta_key_filter( $where ) {
$where = str_replace("meta_key = 'test_repeater_%", "meta_key LIKE 'test_repeater_%", $where);
return $where;
}
add_filter('posts_where', 'add_wildcard_to_meta_key_filter');
//Modify search query
function alter_search_query($query) {
if ( !$query->is_search )
return $query;
$search = $query->query;
$query->set('post_type' ,'page');
$query->set('meta_query', array(
array(
'meta_key' => 'test_repeater_%_test_sub_field',
'meta_value' => '%'.$search.'%',
'compare' => 'LIKE',
),
));
$query->set('s', '');
}
add_action('pre_get_posts','alter_search_query');
It works fine if I add the exact value, i.e. "beef tenderloin", but if I enter "beef" it fails. How do I get the search to be more generic?
I installed the Query Monitor plugin and noticed I got the following SQL
SELECT SQL_CALC_FOUND_ROWS wpfb_posts.ID
FROM wpfb_posts
INNER JOIN wpfb_postmeta
ON ( wpfb_posts.ID = wpfb_postmeta.post_id )
WHERE 1=1
AND ( ( wpfb_postmeta.meta_key LIKE 'test_repeater_%_test_sub_field'
AND wpfb_postmeta.meta_value IN ('beef') ) )
AND wpfb_posts.post_type = 'page'
AND (wpfb_posts.post_status = 'publish'
OR wpfb_posts.post_status = 'acf-disabled'
OR wpfb_posts.post_status = 'private')
GROUP BY wpfb_posts.ID
ORDER BY wpfb_posts.post_date DESC
LIMIT 0, 10
If I change this:
AND ( ( wpfb_postmeta.meta_key LIKE 'test_repeater_%_test_sub_field'
AND wpfb_postmeta.meta_value IN ('beef') ) )
To this:
AND ( ( wpfb_postmeta.meta_key LIKE 'test_repeater_%_test_sub_field'
AND wpfb_postmeta.meta_value LIKE '%beef%' ) )
And run that in phpMyAdmin's SQL, I get the desired row. The question is, how can I change the query to look like that using WordPress functions?
Share Improve this question edited Sep 7, 2017 at 18:24 Jordan Carter asked Sep 7, 2017 at 15:13 Jordan CarterJordan Carter 2912 gold badges5 silver badges12 bronze badges 02 Answers
Reset to default 2You need to add Wildcards to your meta_value.
Change
$query->set('meta_query', array(
array(
'meta_key' => 'test_repeater_%_test_sub_field',
'meta_value' => $search,
'compare' => 'LIKE',
),
));
To
$query->set('meta_query', array(
array(
'meta_key' => 'test_repeater_%_test_sub_field',
'meta_value' => '%'.$search.'%',
'compare' => 'LIKE',
),
));
first you need to loop how many times repeater has values example
anada_number_0_anada_number
anada_number_1_anada_number
anada_number_2_anada_number
In my case i have five times repeater field
for( $i=0; $i<5; $i++ ){
$meta_query['anada_number'][] = array(
'key' => 'anada_number_'.$i.'_anada_number',
'value' => $anada_number,
);
}
then use it in query arg
$arg = array(
$meta_query['anada_number']
);
$wp_query = new WP_Query($arg);