The forums are closed and will be removed when we launch our new site.
Looking for support? You can access the support system via your account.
The forums are closed and will be removed when we launch our new site.
Looking for support? You can access the support system via your account.
Forums › Forums › Search & Filter Pro › Restrict results to date range
Hi Ross,
my (custom) posts feature deals and offers, and these have an expiry date. I would like to only show search results which have not expired yet.
The expiry date field that’s already in each post as standard (not ACF) is being recognised by your plugin so that’s great. Any ideas on how I would do this?
Many thanks in advance,
Philip.
Hey Philip
Add this code to the bottom your functions.php file in your theme
add_action( 'pre_get_posts', 'filter_post_dates' );
function filter_post_dates($query)
{ //this function changes the order of your posts by date
if ( !is_admin() && $query->is_main_query() ) {
$query->set( 'order', 'ASC' ); //a
$query->set( 'orderby', 'date' );
add_filter( 'posts_where', 'after_date_filter');
}
return $query;
}
function after_date_filter( $where = '' )
{ //here we limit the results to only the posts set in from today onwards:
$today = date( 'Y-m-d' );
$where .= " AND post_date >= '$today'";
return $where;
remove_filter( 'posts_where', 'after_date_filter');
}
There is one thing though, I think that posts published in the future (ie with post date) are not considered published
by wordpress so they not display.. but I guess you already know that… In this case you may have to use the custom field, which means the code above will need to be different (let me know and I’ll look)…
Just to note, your results page (once you submit the search form) does not match the layout of your initial search page… let me know if you need help with that I have some recommendations on the best way to set it up.
Thanks
Hey Philip, sorry I’m a bit confused, is the code working? I just realised that this code would apply to all queries on your site, and not just S&F, so I would update it with this:
add_action( 'pre_get_posts', 'filter_post_dates', 21 );
function filter_post_dates($query)
{
global $sf_form_data;
if ( !is_admin() && $query->is_main_query() && $sf_form_data->is_valid_form() ) {
$query->set( 'order', 'ASC' );
$query->set( 'orderby', 'date' );
add_filter( 'posts_where', 'after_date_filter');
}
return $query;
}
function after_date_filter( $where = '' )
{
$today = date( 'Y-m-d' );
$where .= " AND post_date <= '$today'";
return $where;
remove_filter( 'posts_where', 'after_date_filter');
}
But just to note, this code only applies once you have pressed search once on your form (even if its blank)…
The forum ‘Search & Filter Pro’ is closed to new topics and replies.
© 2024 Search & Filter Support. All Rights Reserved.
This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
We also use cookies to store items in your cart as well as allowing your to login on the site.
You can adjust all of your cookie settings by navigating the tabs on the left hand side.
By continuing to use this site, you also agree to our Privacy Policy.
Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.
*Other cookies used for logging in and cart functions will only be used when you use those features and cannot be disabled.
If you disable these cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.
This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.
Keeping this cookie enabled helps us to improve our website.
Please enable Strictly Necessary Cookies first so that we can save your preferences!
More information about our Cookie Policy