Query by 2 values of a repeater ACF field

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.

Read More

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?

Related posts

Leave a Reply

2 comments

  1. 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:

        function add_cond_to_where( $where ) {
    
            //Replace showings_$ with repeater_slug_$
            $where = str_replace("meta_key = 'showings_$", "meta_key LIKE 'showings_%", $where);
    
            return $where;
        }
    
    add_filter('posts_where', 'add_cond_to_where');
    

    Query:

    //Replace showings_$ with repeater_slug_$
    //Replace _discounts with _sub_element_slug
        $args = array(
            'meta_query'        => array(
                array(
                    'key'       => 'showings_$_discount',
                    'value'     => 'students',
                    'compare'   => '='
                )
            )
        );
    

    Reference: https://www.advancedcustomfields.com/resources/query-posts-custom-fields

    I hope this helps,

    Cheers!

  2. 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);