0

I facing a problem with a meta_query where the meta_key's value can have multiple dates :

My meta field event_dates can have several dates like : 20220902,20220928

CODE EXPECTED :

$today = date( "Ymd");

$args = array(
    'post_type' => 'events',
    'meta_query' => array(
        array(
            'key'       => 'event_dates',
            'value'     => $today,
            'compare'   => '>='
        )
    )
);

With the help of other wpse's questions like here, I've try things like :

  ...
  'value'     =>  serialize( intval( $today ) ),
  ...

  // AND/OR
  // in the ACF admin field ("event_dates")
  a:1:{i:0;i:20220902;i:1;i:20220928;}

  // OR 
  20220902,20220928

I also try like a post I can't find again said :

foreach($event_dates as $dates) :

    add_post_meta( $inserted_id, 'event_dates', $dates );

endforeach;

My goal is to find posts where in this meta field (at least) one of those dates is older than today.

Some help please !! :)

3
  • 2
    you can't use event_dates for ordering if it's storing a string of serialised PHP, the values have to be numbers, and singular ( not inside an array or list ). I don't know if ACF will let you store them that way but that's an ACF problem and 3rd party plugin/ACF questions are offtopic here. The most you could hope for is a semi-reliable contains to test for the existence of the data as a filter, but you can never order or use logic conditions such as >= on serialised data
    – Tom J Nowell
    Oct 4, 2022 at 15:02
  • In fact they are store are number ie : 20221012
    – nico
    Oct 4, 2022 at 17:27
  • and it shows that in the database if you look at the raw value instead of using PHP to check? Not a:1:{i:0;i:20220902;i:1;i:20220928;}? And it is only 1 number, not a comma separated list? If there are multiple values stored in the same meta value then it is a serialised data structure, it's just serialised as a CSV instead of PHP, you should not store structured data in post meta if you plan to query/order/sort by it, wether that's something simple like a list, or complicated such as objects/JSON/XML/etc
    – Tom J Nowell
    Oct 4, 2022 at 17:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.