Forums Forums Search & Filter Pro Using choice fields to filter preset date ranges

Viewing 1 post (of 1 total)
  • Trevor
    #270878

    With a date field, there are only two ways I can think to do this.

    1. Have an additional custom field in the posts where one of those 3 labels is the choice.
    2. Use code. How this would work is:

    # Set the form UI field to a number range field, like this (in this case I chose a radio field, but a dropdown would also work, I think):

    https://www.screencast.com/t/rpY6G9KLnX

    Ignore my field name! Use what you have as a field name.

    Then add code like this to your child theme functions.php file (change the field name, form ID number and dates to work for you):

    function update_field_options($input_object, $sfid){
    	
    	// ensure we are only filtering the correct field name in the correct form - in this case the field we want to filter has the name '_sfm_first_date_to', based on the field name 'first_date_to'
    	if( $input_object['name'] == '_sfm_first_date_to' && $sfid == 42 ) {
    		
    		// if we want to filter the options generated, we need to make sure the options variable actually exists before proceeding (its only available to certain field types)
    		if( ! isset( $input_object['options'] ) ) {
    			return $input_object;
    		}
    		//udpate this field before rendering
    		//all the options are stored in $input_object['options'] as an array
    		$new_options = array();
    
    		//create a new "default" option
    		$new_option = new StdClass();
    		$new_option->value = "";
    		$new_option->label = "All Ages";
    		array_push($new_options, $new_option);	
    	
    		//create a new range option we want to add
    		$new_option = new StdClass();
    		$new_option->value = "19900101+29991231"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "Latest"; //the label can be anything
    		array_push($new_options, $new_option);//create a new range option we want to add
    
    		$new_option = new StdClass();
    		$new_option->value = "0+19561231"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "Golden Age"; //the label can be anything
    		array_push($new_options, $new_option);
    
    		$new_option = new StdClass();
    		$new_option->value = "19570101+19891231"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "Silver Age"; //the label can be anything
    		array_push($new_options, $new_option);
    
    		//now replace the options with our own custom options:
    		$input_object['options'] = $new_options;
    	}
    	return $input_object;
    }
    
    add_filter('sf_input_object_pre', 'update_field_options', 10, 2);

    You can see what it looks like on this demo I made (it is not functional as the data does not match!!):

    https://sandfdev.cdnwebservices.net/shop/

    I hope that makes sense!

    It assumes that the date data is correctly stored in ACF/SQL date format(YYYYMMDD).

Viewing 1 post (of 1 total)