1

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?

2

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.