I have many posts, each post is an event with a period . This period can be 3 hours, 3 days, 15 days, 3 month. I defined the start time of event with start_timestampevents and the end with end_timestampevents. These data come from postmeta. I have four buttons : "first week", week +1, week +2 and year list. For the moment I'm focusing on the "current week"
Until now I thought my meta_query do what I wanted but it's not. Because in my meta_query ('meta_query' => array( array())
) I compared the start_timestampevents ('key'=>) with the period of first day until the last day of current week ('value'=>).
I did the same comparison with end_timestampevents
.
Assuming that 'value' => array($firstdayweek_ts,$lastdaymidnight_ts)
get the date (timestamp) of each day between 18 to 25 September, I'm not comparing things in the right way.
I keep the week from 18 to 25 September 2023 and event from 12 to 25 September for below example *.
My goal is to compare each timestamp/date of the event period (12,13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25) with these of week range (18, 19, 20, 21, 22, 23, 24).
Until now, my events have only been displayed for the range of days in this week* if the start and end dates of this event are within it. Start and end dates outside (before or after) this weekly period are ignored, but the event always takes place during this week.
According to wp documentation, WP_Query
or get_posts()
doesn't allow to use array inside 'key'=>
parameter .
I need to compare all the days (timestamps) of the event with the range of days in the current week*, so that at least one timestamp of the event must correspond to one of the days in the week after using strtotime().
array(7) { [0]=> string(10) "18-09-2023" [1]=> string(10) "19-09-2023" [2]=> string(10) "20-09-2023" [3]=> string(10) "21-09-2023" [4]=> string(10) "22-09-2023" [5]=> string(10) "23-09-2023" [6]=> string(10) "24-09-2023" }
the same array with timestamp :
array(7) { [0]=> string(10) "1694988000" [1]=> string(10) "1695074400" [2]=> string(10) "1695160800" [3]=> string(10) "1695247200" [4]=> string(10) "1695333600" [5]=> string(10) "1695420000" [6]=> string(10) "1695506400" }
Must I repeat array('key'
for each date of event period range ?
If yes can I do a loop from array of dates event ?
My query:
$ActivitiesType = array(
'post_type' => $activityType,
'post_status' => 'publish',
'posts_per_page' => -1,
//'meta_key' =>'start_timestampevents',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'end_timestampevents',
'value' => array($firstdayweek_ts,$lastdaymidnight_ts),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
),
array(
'key' => 'start_timestampevents',
'value' => array($firstdayweek_ts,$lastdaymidnight_ts),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
)
);
2022-01-31 12:00:00 UTC
, and they cannot be structured data. No arrays, no objects, no JSON, no XML, etc, MySQL cannot search inside an array for values, they just get stored as a single serialized string. You can attempt to do it by searching for substrings but you'll get lots of false positives, and you can't do comparisons likeBETWEEN
or perform logic. Note that meta keys are not unique, you can have multiple independent meta values with the same key