Support

Account

Home Forums Add-ons Repeater Field Why is my meta query only looking at the first index of my repeater field

Solving

Why is my meta query only looking at the first index of my repeater field

  • I am using Advanced Custom Fields Pro and I have a repeater field in my custom post type that has multiple starting dates. So the repeater fields is called date and has starting date so the hierarchy would look like this.

    date
    –startingdate

    So a post can have multiple starting dates. I am using a meta query to get all the posts that start after the date of today.

    $posts = get_posts(array(
        'numberposts'   => $limit,
        'post_type'     => 'events',
        'orderby'           => 'meta_value',
        'meta_query' => array(
             array(
                'key'     => 'date_0_startingdate',
                'value'   => $today,
                'compare' => '>',
            )
        ),
        'order' => $order,
    ));

    But I noticed that it’s only looking at the first index of the repeater field. How can I make sure I looks at all the fields and take the post if there is one date that starts after today.

    If a take a custom post and go to the date repeater field and set it like this

    1. date before today
    2. date after today
    it won’t find the post. But it should because index 2 is a date after today.

    If I set it like this

    1. date after today
    2. date before today
    It will find it.

  • Because you’re using date_0_startingdate and 0 represents the first row of the repeater.

    WP has recently added the ability to query the meta key using “LIKE” https://make.wordpress.org/core/2019/01/23/like-support-for-meta-keys-in-5-1/, but to be honest, I don’t think it will work on repeaters and you’re probably still going to need to use the example from this doc https://www.advancedcustomfields.com/resources/query-posts-custom-fields/ there is a section on repeater sub fields there.

    But, then again, I would do this https://acfextras.com/dont-query-repeaters/, but if you’ve already got a lot of data in the site it would mean updating every post. It’s something that you need to plan for ahead of time.

  • I added

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

    to my functions.php and set supress filters to false like so

    $posts = get_posts(array(
    	'numberposts'	=> $limit,
    	'post_type'		=> 'opleidingen',
    	'orderby'			=> 'meta_value',
    	'suppress_filters' => 'false',
    	'meta_query' => array(
    			array(
    			'key'     => 'data_$_startdatum',
    			'value'   => $today,
    			'compare' => '>',
    		)
    	),
    	'order'	=> $order,
    ));

    But it’s getting 0 results?

  • What is the value of $today?

    Also, you said it’s a date field, but I want to be sure, is it a date field or a date time field?

  • $today = date('Ymd');

    But it worked like a charm with _0_ for just the first value. Now $posts finds 0 values. I also tried to change the comparer to ‘<‘ but that did not work.

  • you have a type in your meta key data instead of date

  • No I didn’t translate the code this time! It’s dutch. I didn’t change the meta_key.

  • Then I don’t know. It looks like it should be working by the documentation, but like I said, I never query by repeater sub fields.

  • Does it have anything to do with the fact that I run it from a code snippet made with the code snippets plugin? Or do you know anyone who would know what’s wrong? I could really use the help

  • I am referring to this part

    in your original code

    
    ->           |    |
    'key'     => 'date_0_startingdate',
    

    in your new code

    
    ->          |    |
    key'     => 'data_$_startdatum',
    
  • The repeater field is called data and the the subfield is called startdatum. I just translated it in my origional post so it would be easier to understand.

  • did you add this part

    
    add_filter('posts_where', 'add_cond_to_where');
    
  • Yes, I double checked just now. But I did add it.

    function add_cond_to_where( $where ) {
    
        $where = str_replace("meta_key = 'data_$", "meta_key LIKE 'data_%", $where);
    
        return $where;
    }
    
    add_filter('posts_where', 'add_cond_to_where');

    Is in my functions.php. Anyway I can test if it works?

Viewing 13 posts - 1 through 13 (of 13 total)

The topic ‘Why is my meta query only looking at the first index of my repeater field’ is closed to new replies.