最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Query by 2 values of a repeater ACF field

programmeradmin4浏览0评论

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?

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?

Share Improve this question asked Apr 12, 2014 at 0:52 MikOMikO 5951 gold badge6 silver badges17 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3

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/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

    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);
发布评论

评论列表(0)

  1. 暂无评论