Forums Forums Search & Filter Pro Filter by year and separate sort control

Viewing 9 posts - 1 through 9 (of 9 total)
  • Anonymous
    #271022

    split by @trevorsf from https://support.searchandfilter.com/forums/topic/filter-by-year-and-separate-sort-control/

    Would this sometime in the New Year be something like January? 🙂 We really need the year only drop-down option.

    Do you have a workaround until V3 comes out for the year only? I know above you said “if you had a custom field’. DO you have more instructions for that? How do we set that up?

    Trevor
    #271054

    At this time, with the other commitments we have, the earliest I expect V3 would be late February, but that may move back a few weeks.

    Is the date field that you want to use only going to be used once in the form, and are the dates stored in the database in the SQL/ACF format (YYYYMMDD)? If the answer to both is yes, a manually coded solution is possible, which I could show you.

    Otherwise, the coding gets more complex, needing a second custom field for the year, where the value is auto calculated each time you save a post, as per this post:

    https://support.searchandfilter.com/forums/topic/datefilter/page/3/#post-238869

    Anonymous
    #271111

    Yes, the date is only used once in the form and yes YYYYMMDD is the format. Can you show us the manually coded solution, please? Thank you

    Trevor
    #271160

    OK, and for anybody who has the same conditions that the previous user has, here is a demo form:

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

    There are 5 ‘posts’, each has one ‘date’, each in a different year (2016-2020 inclusive).

    The ‘Year’ field is based on a custom field named first_date_from, and this is an ACF date field with a single date saved in the format YYYYMMDD.

    The code below is pasted in to the child theme functions.php file:

    function update_date_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_from', based on the field name 'first_date_from'
    	if( $input_object['name'] == '_sfm_first_date_from' && $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 Years";
    		array_push($new_options, $new_option);	
    	
    		//create a new range option we want to add
    		$new_option = new StdClass();
    		$new_option->value = "20160101+20161231"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "2016"; //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 = "20170101+20171231"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "2017"; //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 = "20180101+20181231"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "2018"; //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 = "20190101+20191231"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "2019"; //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 = "20200101+20201231"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "2020"; //the label can be anything
    		array_push($new_options, $new_option);//create a new range option we want to add
    
    		//now replace the options with our own custom options:
    		$input_object['options'] = $new_options;
    	}
    	return $input_object;
    }
    
    add_filter('sf_input_object_pre', 'update_date_field_options', 10, 2);

    I hope you can see how it works by actually sending a date range of 1 Jan to 31 Dec of each year to the form. So, if you want more years, add them as you need.

    This is how the field is set:

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

    Anonymous
    #271330

    I tried it and it is not working correctly.

    How did you set up the ACF date field? Datepicker? Can you send a screen shot?

    In the dropdown, I am only seeing…

    All Years
    0-1
    1-2
    2-3

    Thank you

    Trevor
    #271334

    What is the field name? Are you able to send me a live link/URL to your search page so I can take a look?

    Anonymous
    #271423

    The ACF date picker field name we set up is first_date_from. Above you wrote “custom field named first_date_from, and this is an ACF date field” Should it be set up differently?

    The site is behind google authentication during development. I can’t share a link at this time.

    Trevor
    #271429

    Ah. You could have named it anything you liked. My code was simple based on what was on my site.

    When you are at a point that you can share access (privately of course), then I would need to take a look. I can’t really help much more at this point.

    Trevor
    #271431
    This reply has been marked as private.
Viewing 9 posts - 1 through 9 (of 9 total)