We are using the Search & Filter Pro plugin on our WordPress site and need to be able to restrict results for two particular categories by preset dates. I am trying to accomplish this without having to manually edit over 1000 posts to change the category or add a category, or require us to tag it a certain way to separate the posts into eras.
My preference is to restrict the dates that can be entered in the form with a min date on some forms and a min and max date on others. In version 2, I don’t see a way to set a min and/or max date on the date range control in the search form and can’t even begin to figure out how to do that in code (PHP or JS). It would greatly improve the user experience if users can’t enter a date outside the range so that the results match the entered dates. I can filter behind the scenes using the below code.
I have managed to find a code snippet, and modified it, for altering the query for specific S&F Pro forms:
function filter_media_release_current_term( $query_args, $sfid ) {
if($sfid==2530 || $sfid==2488)
{
$currentafter = 2025;
$startyear = '2025';
$aftermonth = '01';
$afterday = '01';
$currentbefore = '2025';
// edit later to use a full date, not just year
// if no date query set the after value
if (!isset($query_args['date_query'])) {
$query_args['date_query'] =
array(
'after' => array (
'day' => $afterday,
'month' => $aftermonth,
'year' => $startyear,
),
'inclusive' => true,
);
} else {
// One or both dates are entered: see if they entered a 'To:' date and grab those values
if (isset($query_args['date_query']['before']) && isset($query_args['date_query']['before']['year'])) {
$currentbefore = $query_args['date_query']['before']['year'];
$beforeday = $query_args['date_query']['before']['day'];
$beforemonth = $query_args['date_query']['before']['month'];
if ($currentbefore < $startyear) {
// can't return any results prior to the start date - resets the date query to default
print_r("<br/><span style='color: red'> Enter dates starting " . $aftermonth ."/" . $afterday . "/" . $startyear . "</span><br/><br/>");
$query_args['date_query'] =
array(
'after' => array (
'day' => $afterday,
'month' => $aftermonth,
'year' => $startyear,
),
'inclusive' => true,
);
} else {
// Before date is valid
// Did they enter a 'From:' date?
if (isset($query_args['date_query']['after']) && isset($query_args['date_query']['after']['year'])) {
$currentafter = $query_args['date_query']['after']['year'];
// if before start date - reset it to the start date but keep the before entry
if ($currentafter < $startyear) {
print_r("<br/><span style='color: red'> Start date out of range!</span><br/><br/>");
$query_args['date_query'] =
array(
'after' => array (
'day' => $afterday,
'month' => $aftermonth,
'year' => $startyear,
),
'before' => array (
'day' => $beforeday,
'month' => $beforemonth,
'year' => $currentbefore,
),
'inclusive' => true,
);
} // no else here - do nothing if both dates are in the valid range
} else {
// no 'From:' date entered - use default from date and the 'To:' date they entered
$query_args['date_query'] =
array(
'after' => array (
'day' => $afterday,
'month' => $aftermonth,
'year' => $startyear,
),
'before' => array (
'day' => $beforeday,
'month' => $beforemonth,
'year' => $currentbefore,
),
'inclusive' => true,
);
}
}
} else {
// they entered only the 'From:' value but make sure it is set to avoid errors
if (isset($query_args['date_query']['after']) && isset($query_args['date_query']['after']['year'])) {
$currentafter = $query_args['date_query']['after']['year'];
// if before start date - reset it to the start date but keep the before entry
if ($currentafter < $startyear) {
print_r("<br/><span style='color: red'> Start date out of range!</span><br/><br/>");
$query_args['date_query'] =
array(
'after' => array (
'day' => $afterday,
'month' => $aftermonth,
'year' => $startyear,
),
'inclusive' => true,
);
} // no else here - do nothing if the date is in the valid range
} else {
// no year set for some reason
$query_args['date_query'] =
array(
'after' => array (
'day' => $afterday,
'month' => $aftermonth,
'year' => $startyear,
),
'inclusive' => true,
);
}
}
}
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_media_release_current_term', 20, 2 );
This successfully filters the results, ensuring no results prior to 1/1/2025 are shown even if they don’t filter by date range. However, I had to turn off ajax results so that the page reloads on each submit instead of without reloading because it didn’t go through this function with ajax updates. Also, I can’t figure out how to update the form or the URL from this function so the page displays “Showing results from [date] to [date]” using the original dates.
How do I apply fix the UI problems?
Thank you for any guidance you can give me.