0

For a custom plugin with add start and end date of event/post, I must generate several datetime with the same meta key to compare the period of event with the period of weeks. I my case I'm obliged to compare each day date of event with these of period because the 'key'=>of meta_query can't be a array. The user will have the choice with current week, week +1 and week+ 2 and 3.

I created a script to generate all days between start and end of event. Depending on my custom plugin script to add/update several _post_meta(), I include each meta key inside an array.

The period event is from 12 to 28 September : my script generate these date :

2023-09-12 00:00:00
2023-09-13 00:00:00
2023-09-14 00:00:00
2023-09-15 00:00:00
2023-09-16 00:00:00
2023-09-17 00:00:00
2023-09-18 00:00:00
2023-09-19 00:00:00
2023-09-20 00:00:00
2023-09-21 00:00:00
2023-09-22 00:00:00
2023-09-23 00:00:00
2023-09-24 00:00:00
2023-09-25 00:00:00
2023-09-26 00:00:00
2023-09-27 00:00:00
2023-09-28 23:59:59

Each date ($date) is converted into a datetime to obtain a timestamp. In meta_query these dates are compared with those of the selected week :

from 2023-09-18 00:00:00 to 2023-09-24 00:00:00

How can I output these datetime allocating a variable to each to obtain as many times this variable as there are dates generated $events_meta['timestamps_between'] = strtotime($date);

foreach ($period as $date) {
    $events_meta['timestamps_between'] .= strtotime($date);
}

My script call many meta_key and I called them inside this foerreach :

foreach ( $events_meta as $key => $value ) {
    add_post_meta( $post->ID, $key, $value );
    //if($key = 'timestamps_between'){
        //print_r($key .' and '. $value); 
    //}
}

Online test

1 Answer 1

0

Problem Solved but solution not reusable.

I'd like some advice or a solution to assign the same meta_key to each datetime generated.

Based from this answer : to assign variables to each datetime from foreach loop I do that (but I think it's a bad idea to change the name of the array keys using the is_int() function to target the key integers):

  1. create an array ($events_meta)
  2. Inside a foreach loop, call the dates of DatePeriod

My solution is only usable because I only use is_int for the 'timestamps_between' meta_key.

$start = $beginOfDayObject;
$interval = new DateInterval('P1D');
$end = $excludeObject;
$period = new DatePeriod($start, $interval, $end);

$events_meta = array();
foreach ($period as $date) {
    $events_meta[] = $periodtotime."\n";
}

each new meta_key will be read by this foreach loop: (The condition using is_int() allow to replace all keys integer by the meta_key.)

foreach ( $events_meta as $key => $value ) {
        if(is_int($key) ) $key ='timestamps_between';
         add_post_meta( $post->ID, $key, $value );
}

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.

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