Forums Forums Search & Filter Pro Restrict results to date range

Viewing 10 posts - 1 through 10 (of 27 total)
  • Anonymous
    #1677

    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.

    Anonymous
    #1678
    This reply has been marked as private.
    Ross Moderator
    #1679

    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

    Anonymous
    #1680
    This reply has been marked as private.
    Ross Moderator
    #1681

    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)…

    Anonymous
    #1683
    This reply has been marked as private.
    Ross Moderator
    #1684
    This reply has been marked as private.
    Anonymous
    #1686
    This reply has been marked as private.
    Ross Moderator
    #1688
    This reply has been marked as private.
    Ross Moderator
    #1693
    This reply has been marked as private.
Viewing 10 posts - 1 through 10 (of 27 total)