Forums Forums Search & Filter Pro Filter events by upcoming

Viewing 10 posts - 1 through 10 (of 11 total)
  • Trevor
    #183150

    At the moment, our plugin’s date comparison (operator) options do not include >= a give date, but you should be able to modify the operator in the query before the search is actually made using our Edit Query Arguments filter, as long as you always want it to work this way. Here is the documentation:

    https://searchandfilter.com/documentation/action-filter-reference/#edit-query-arguments

    Anonymous
    #183172

    Ok so just to clarify, the search form will display all the events (past, present, and future) when none of the filters have been used. Users are able to enter keywords, change the category etc, and all events that are applicable will still be shown. Only when the date filter is modified, due to the edit query args filter, all the past events will disappear. Is that correct?

    Trevor
    #183184

    Assuming you code the PHP when using that filter to look to see if the date has been used, and then change the operator, yes.

    Anonymous
    #183232

    Ok, awesome! I took a stab at it today and I’ve written this which pretty much works backwards. It removes all the past events on page load and maintains that when using the keywords and category filter. Then when I enter in a date it reverts to showing just the events that happen on that date, nothing upcoming. Would you be able to help me alter it to achieve what I need? I’m also not sure how to pull the users input from the form, or how to check a specific field if it has been utilized. I apologize if I’ve gone completely down the wrong track!

    function upcoming_events( $query_args, $sfid ) {
    
    	if ( $sfid == 72 ) {
    		$dateField = ''; //get users input, if it exists
    
    		if ( $dateField != '' ) {
    
    			$query_args['meta_query'] = array( 
    				array(
    					'key' => 'start_date', 
    					'value' => $dateField, 
    					'compare' => '>=', 
    					'type' => 'NUMERIC' 
    				)
    			);
    		}
    	}
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'upcoming_events', 20, 2 );
    Trevor
    #183257

    Why not output the $query_args array when the user has selected a date to see what it looks like?

    Like this:

    echo '<pre>';
    print_r($query_args);
    echo '</pre>';
    Anonymous
    #183380
    This reply has been marked as private.
    Trevor
    #183417
    This reply has been marked as private.
    Ross Moderator
    #183555

    Hi Jessica

    Can you break down exactly what you want to do.

    From reading above, my impression is:

    1) You want to hide all upcoming or past posts, if a user has not selected anything in the date filter.

    2) But, you want them to be able to select any date, effectively bypassing the above mentioned restrictions to the search?

    I think (hope) this might be easier than we’re going about it.

    Why not, in your filter above, check to see if if the user has interacted with the date field, if not apply your logic?

    This would be as simple as checking

    if(!isset($_GET['your_field_name'])){
        //do stuff
    }
    //always return the query_args even if not modifyied
    return $query_args;

    Thanks

    Anonymous
    #183569

    Thank you both so much for your help, I really appreciate it!

    I would like for all of the events to show as normal, and have all the keyword, category etc filters work as normal, I just want to change the behaviour of the date filter.

    Once a date is selected I would like the results to only include events that occur on that date (which it does already), but to also show events that occur after that date.

    I’ve updated my previous query to this which I think should work, except that it seems the date filter’s original programming is overriding my meta query.

    function upcoming_events( $query_args, $sfid ) {
    
    	if ( $sfid == 72 ) {
    
    		if(isset($_GET['_sfm_start_date'])){
    			$dateField = $_GET['_sfm_start_date'];
    
    			$query_args['meta_query'] = array( 
    				array(
    					'key' => 'start_date', 
    					'value' => $dateField, 
    					'compare' => '>=', 
    					'type' => 'NUMERIC' 
    				)
    			);
    		}
    	}
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'upcoming_events', 20, 2 );

    By using the print_r($query_args); I am able to see that the query does run when a date is entered, except it seems to be ignored as it doesn’t have any effect on the results. The results still only show events that occur on the exact date which is entered.

    How do I go about overriding the date filters current programming?

    Ross Moderator
    #183615

    Ah ok, I understand.

    I’m afraid the way you are going about it won’t work.

    When using most of S&F’s filters, we actually don’t create a standard query.

    We perform some background queries, to get the post IDs, of all the results of that date.

    It is then fed into the WP_Query, in the post__in argument of the query you have been trying to modify above, BUT, when a single date is selected, the post__in part will only have the IDs of results on that date, so any additional meta query won’t pull in the extra IDs needed.

    Anyway, fear not, for now there is a workaround.

    What you’ll want to do, is create a date range, and set the second (to) field (via programming) of the date range, to some point far in the future. Then you’ll need to hide the second date field with CSS.

    The user will only see the from field, and choose a specific day, but in actuality its comparing between today, and some point in the far future.

    I explained how to set the value of the second date field via PHP in this reply:
    https://support.searchandfilter.com/forums/topic/infinite-scroll-not-working-on-initial-load/page/3/#post-182076

    I hope all that makes sense – let me know if you have any further questions.

    Thanks

Viewing 10 posts - 1 through 10 (of 11 total)