Use the attribute value to decide today and tomorrow like:
Code explanation: Instead of today and tomorrow naming in shortcode, I’m using show_for in this answer. Based on show_for value we can update the meta query later on. In the code, I checked if show_for is set by user or it exists in the $atts
array. If yes, I set the $prog_time
variable based on the $atts['show_for']
value. If no, I set the default value for $prog_time
.
if(isset($atts['show_for'])) {
switch($atts['show_for']) {
case 'today' : $prog_time = current_time("Y-m-d"); break;
case 'tomorrow' : $prog_time = date("Y-m-d", strtotime('tomorrow')); break;
default : $prog_time = current_time("Y-m-d"); break;
}
} else {
$prog_time = current_time("Y-m-d");
}
Now, there is no need to use $today
and $tomorrow
separately. Use only $prog_time
instead of $today
in the meta query.
'meta_query' => array(
array(
'key' => 'prog_date_time',
'value' => $prog_time, // updated here; will have dynamic time as decided in switch clause
'compare' => '=',
'type' => 'DATE',
),
),
Your shortcode call will be like:
[prognostika_home_loop show_for="today"]
[prognostika_home_loop show_for="tomorrow"]
- and you can configure many in switch clause for passed, yesterday, etc.
Additional Notes:
- Since you’re using
Y-m-d
format, make sureprog_date_time
(while saving post meta) value is also set/updated with the same format for matching. - If you’ve multiple attributes, setting default values for attributes is recommended.