Support

Account

Home Forums Backend Issues (wp-admin) Query by repeaterfield subfield(s)

Solved

Query by repeaterfield subfield(s)

  • Hi,
    I am scratching my head if this is even possible. I have a CPT with a repeater field called “dates”. The repeater contains multiple fields but I’m only interested in the field “date” (value returns as ‘Ymd’).

    On my archive page I wan’t to modify the main query to only output those posts that have a date that lies in the future. I can query the value of the first date with its index like so:

    
    $query->set('meta_query',
    array(
      'relation' => 'AND', 
        array(
          'key'  => 'dates_0_date',
          'value' => date('Ymd'),
          'compare' => '<='
        ),
      ),
    );
    
    $query->set('meta_key', dates_0_date');
    $query->set('orderby', 'meta_value');
    $query->set('order','ASC');
    

    But I have variable number of dates per post. So when I wan’t to query by the lowest “date” field that is still in the future I am completely lost.

    I’ve tried to use a wildcard on the key like ‘dates%date’ but that doesn’t work.
    Does anyone know if and how this is possible to achieve?

    Thanks for any help or tips!

  • Hi John, thanks for the link. I’ve looked at that page but didn’t realise my problem was covered there. I’ve managed to get it to work but honestly have no clue why/how it works.

    Thanks again!

  • It has to do with changing the WHERE part of the query from something like
    WHERE meta_key = "dates_0_date"
    to
    WHERE meta_key LIKE "dates_%_date"

  • OMG I was only focused on the key/value comparison and was wondering how I could compare two dates with “LIKE” … I am stupid!

    Thanks again

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

You must be logged in to reply to this topic.