Hey there guys,
I have a post type called “Manuals” with a repeater field called “Files” – within the repeater are subfields called “Serial # Start” & “Serial # End”. I have a search form that I’m trying to pull results for when the user enters a serial number in between or equal to these two values. I’m starting with http://www.advancedcustomfields.com/resources/how-to-query-posts-filtered-by-custom-field-values/#example-5 as a base but running into some issues…
Here’s my current query:
$args = array(
'post_type' => 'manual',
'numberposts' => - 1,
'meta_query' => array(
array(
'key' => 'files_%_start_range_num',
'value' => $_GET['s'],
'compare' => '>='
),
array(
'key' => 'files_%_end_range_num',
'value' => $_GET['s'],
'type' => 'NUMERIC',
'compare' => '<='
)
),
);
The filter is I believe where the issue lies:
function acf_posts_where( $where ) {
$where = str_replace( "meta_key = 'files_%_start_range_num'", "meta_key LIKE 'files_%_start_range_num'", $where );
// $where = str_replace( "meta_key = 'files_%_end_range_num'", "meta_key LIKE 'files_%_end_range_num'", $where );
return $where;
}
//add_filter( 'posts_where', 'acf_posts_where' );
Any help or insight would be greatly appreciated.
Hey Guys,
I figured out the issue. Basically, in my previous post I had the greater than & less than comparison switched AND also my filter was not properly string replacing both sql where statements. Here’s the code I have working now, hopefully it helps someone out there:
WP_Query args:
//Serial# Meta Query
$args = array(
'post_type' => 'manual',
'numberposts' => - 1,
'suppress_filters' => false,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'files_%_start_range_num',
'value' => intval( $_GET['s'] ),
'type' => 'NUMERIC',
'compare' => '<='
),
array(
'key' => 'files_%_end_range_num',
'value' => intval( $_GET['s'] ),
'type' => 'NUMERIC',
'compare' => '>='
)
),
);
Filter:
// custom filter to replace '=' with 'LIKE'
// see: http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/
function acf_posts_where( $where ) {
$where = str_replace( "meta_key = 'files_%", "meta_key LIKE 'files_%", $where );
return $where;
}
add_filter( 'posts_where', 'acf_posts_where' );