Let’s say I have a custom post type called Film
, with a repeater field called Showings
, with fields start_datetime
and discount
.
I need to make a query for those Films that have at least one showing that is in the next week and (this showing) has also a discount.
The query I have now, retrieves the films with at least one showing in the next week and at least one showing (maybe other) with a discount. It doesn’t check that these 2 showings are the same!
My query:
$args = array(
'numberposts' => -1,
'post_type' => 'film',
'meta_key' => 'showings_%_start_datetime',
'meta_value' => array( time(), strtotime('+1week') ),
'meta_type' => 'NUMERIC',
'meta_compare' => 'BETWEEN',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'showings_%_discount',
'value' => 'students',
'type' => 'CHAR',
'compare' => '='
)
)
);
I’ve been checking the generated SQL query and I don’t think it’s possible, because the showings, since it’s a repeater field, don’t have a proper ID, so we’d need to check that the number inside the meta_key are the same, for example showings_5_start_datetime
and showings_5_discount
.
Any ideas please?
I know this is an old question, but I thought I’d post the answer in case anybody else is facing the same problem.
functions.php:
Query:
Reference: https://www.advancedcustomfields.com/resources/query-posts-custom-fields
I hope this helps,
Cheers!
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
then use it in query arg