Support Forums

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 Filter events by upcoming

Viewing 10 posts - 1 through 10 (of 12 total)
  • Jessica Spata
    #183126

    Hi,

    I have an event site that I’ve built with ACF and Search and Filter Pro. I’m trying to add a section to my search form that allows users to select a date, specified by a custom field ‘start_date’ created using ACF, which will then output all the events that occur on that date or afterwards. By using the Post Meta – Date field I am able to output all events on that exact date, am I able to modify it to output all the upcoming events as well as the ones on that date?

    Thank you!

    Trevor Moderator
    #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

    Jessica Spata
    #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 Moderator
    #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.

    Jessica Spata
    #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 Moderator
    #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>';
    Jessica Spata
    #183380
    This reply has been marked as private.
    Trevor Moderator
    #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

    Jessica Spata
    #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?

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

The topic ‘Filter events by upcoming’ is closed to new replies.