Crocoblock’s Jet Engine is a very powerful WordPress plugin. I’ve used it create several event directories, and found it works very well for this in the situations where you want a high level of control over the types of data you display, and how they are displayed.
When displaying events, and providing a UI for site visitors to filter the events, it’s often useful to query dates by relative and/or specific time periods. For example, you might want to allow users to filter Events that are coming up this weekend, or events occurring during the current month. Jet Engine’s query builder does allow you to filter by today(), but for a more specific period of time, you can use a couple simple shortcodes.
One particular use case for this would be to use it with JetSmartFilter’s Query Switcher. The Query Switcher let’s you make a Select filter that has multiple queries contained within it.
The Shortcodes
Let’s start with a couple shortcodes that will allow us to build a filter that will let us make a filter that will get Events ocurring this weekend. You can add this code to a child theme’s functions.php file, or create a simple plugin to house them.
This Weekend
The following shortcode returns a timestamp for the upcoming Friday if it is currently Thursday or earlier in the week. If the current day is between Friday and Sunday, it returns the Friday that just passed. For instance, if it’s currently Monday, August 5th, 2024, it will return a timestamp of Friday, August 9th (at midnight). If it’s currently Saturday, August 17th, it will return Friday, August 16th.
function this_friday() {
// Get the current timestamp
$current_timestamp = time();
// Create a DateTime object for the current time
$current_date = new DateTime();
// Find the day of the week (0 for Sunday, 1 for Monday, etc.)
$current_day = $current_date->format('w');
// Check if today is Monday (1) or later
if ($current_day >= 1) {
// Calculate the days until the next Friday
$days_until_friday = (5 - $current_day + 7) % 7;
if ($days_until_friday == 0) {
$days_until_friday = 7; // If today is Friday, look for the next Friday
}
// Add the days to the current date
$current_date->modify("+$days_until_friday days");
// Get the timestamp for the next Friday
$next_friday_timestamp = $current_date->getTimestamp();
// Return the timestamp
return $next_friday_timestamp;
} else {
// Calculate the days since the last Friday
$days_since_friday = ($current_day + 2) % 7; // 2 days back from Sunday to get to Friday
// Subtract the days to get to the last Friday
$current_date->modify("-$days_since_friday days");
// Get the timestamp for the last Friday
$last_friday_timestamp = $current_date->getTimestamp();
// Return the timestamp
return $last_friday_timestamp;
}
}
// Register the shortcode with WordPress
add_shortcode('this_friday', 'timestamp_next_or_last_friday');
This shortcode will do something similar, except that it will return a timestamp for next Sunday at 11:59PM if it’s currently any day before Sunday. If it is currently Sunday, it will return a timestamp for 11:59PM today.
function timestamp_next_or_current_sunday() {
// Get the current timestamp
$current_timestamp = time();
// Create a DateTime object for the current time
$current_date = new DateTime();
$current_date->setTimestamp($current_timestamp);
// Find the day of the week (0 for Sunday, 1 for Monday, etc.)
$current_day = $current_date->format('w');
if ($current_day == 0) {
// If today is Sunday, set the time to 11:59 PM on this Sunday
$current_date->setTime(23, 59);
return $current_date->getTimestamp();
} else {
// Calculate the days until the next Sunday
$days_until_sunday = (7 - $current_day) % 7;
// Add the days to the current date to get the next Sunday
$current_date->modify("+$days_until_sunday days");
// Set the time to 11:59 PM on the next Sunday
$current_date->setTime(23, 59);
// Get the timestamp for 11:59 PM on the next Sunday
return $current_date->getTimestamp();
}
}
// Register the shortcode with WordPress
add_shortcode('this_monday', 'timestamp_next_or_current_sunday');
You can use these shortcodes in the Query Builder by specifying a filter of ‘Field’ (whatever your timestamp field is for the event start datetime) Greater Than or Equal To ‘Shortcode’ [this_friday], and ‘Field’ Less Than or Equal To ‘Shortcode’ [this_sunday].
Other shortcodes
You can also use shortcodes to get Events relative to a number of days. For example, the following shortcodes will return timestamps for a datetime that is 7 days from now, and 30 days from now, respectively.
function seven_days_from_now() {
// Get the current timestamp
$current_timestamp = time();
// Add 7 days (7 * 24 * 60 * 60 seconds)
$future_timestamp = $current_timestamp + (7 * 24 * 60 * 60);
// Return the future timestamp
return $future_timestamp;
}
// Register the shortcode with WordPress
add_shortcode('seven_days_from_now', 'seven_days_from_now');
function thirty_days_from_now() {
// Get the current timestamp
$current_timestamp = time();
// Add 7 days (7 * 24 * 60 * 60 seconds)
$future_timestamp = $current_timestamp + (30 * 24 * 60 * 60);
// Return the future timestamp
return $future_timestamp;
}
// Register the shortcode with WordPress
add_shortcode('thirty_days_from_now', 'thirty_days_from_now');
If you want to make a query to get Events within the next 30 days, you can use ‘Field’ Less than or Equal to ‘Shortcode’ [thirty_days_from_now]. Likewise for Events less than or equal to 7 days from now.
Leave a Reply