Home › Forums › General Issues › WP_Query based comparing TWO repeater field values
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' );
You must be logged in to reply to this topic.
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’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.