Files
tonys-sportspress-enhancements/includes/timeframe-filter.php
2023-12-24 09:07:27 -06:00

70 lines
2.1 KiB
PHP

<?php
/*
Enhances the post management by adding custom providing a dropdown to
filter events based on different timeframes.
*/
// Add the custom filter dropdown
function timeframe_filter_dropdown()
{
$current_screen = get_current_screen();
if ($current_screen->id == "edit-sp_event") {
$selected = isset($_GET["timeframe"]) ? $_GET["timeframe"] : ""; ?>
<select name="timeframe">
<option value="" <?php selected(
"",
$selected
); ?>>All Timeframe</option>
<option value="last3days" <?php selected(
"last3days",
$selected
); ?>>Last 3 Days</option>
<option value="last7days" <?php selected(
"last7days",
$selected
); ?>>Last 7 Days</option>
<option value="plusminus3days" <?php selected(
"plusminus3days",
$selected
); ?>>+/- 3 Days</option>
</select>
<?php
}
}
add_action("restrict_manage_posts", "timeframe_filter_dropdown");
// Modify the query based on the selected filter
function timeframe_filter_query($query)
{
global $pagenow;
if ($pagenow == "edit.php" && isset($_GET["timeframe"]) ) {
if ($_GET["timeframe"] == "last3days") {
$date_query = [
[
"after" => date("Y-m-d", strtotime("-3 days")),
],
];
} elseif ($_GET["timeframe"] == "last7days") {
$date_query = [
[
"after" => date("Y-m-d", strtotime("-1 week")),
],
];
} elseif ($_GET["timeframe"] == "plusminus3days") {
$date_query = [
[
"after" => date("Y-m-d", strtotime("-3 days")),
"before" => date("Y-m-d", strtotime("+3 days")),
],
];
}
if (!$_GET["timeframe"] == "") {
$query->set("date_query", $date_query);
}
}
}
add_action("pre_get_posts", "timeframe_filter_query");