Forums Forums Search Search Results for 'sf_input_object_pre function order'

Viewing 10 results - 1 through 10 (of 18 total)
  • Author
    Search Results
  • #274760

    Ross
    Keymaster

    Hi Danny

    Writing something like that is a little out of scope of support (should require custom development) however I’ve gone ahead and bashed something toegether for you anyway (I won’t be able to customise this for you):

    
    function sf_filter_options_sub_terms( $input_object, $sfid ) {
    
    	$filter_name = '_sft_category';
    	$taxonomy_name = 'category';
    	$parent_term_id = 2; // you need to figure out the ID of the parent
    	
    	if ( $input_object['name'] !== $filter_name ) {
    		return $input_object;
    	}
    	
    	if ( ! isset( $input_object['options'] ) ) {
    		return $input_object;
    	}
    	
    	if ( ! is_array( $input_object['options'] ) ) {
    		return $input_object;
    	}
    	
    	// ask WP for the child terms
    	$child_terms = get_terms( $taxonomy_name, array( 'parent' => $parent_term_id, 'orderby' => 'slug', 'hide_empty' => false ) ); 
    	
    	// init the first option
    	$default_option = new StdClass();
    	$default_option->value = '';
    	$default_option->label = 'Choose a Category';
    	
    	// setup the new options array
    	$child_options = array( $default_option );
    	
    	// grab a reference to the search form
    	global $searchandfilter;
    	$search_form = $searchandfilter->get( $sfid );
    	
    	// loop through child terms
    	foreach ( $child_terms as $child_term ) {
    		
    		// add back in the dynamic count
    		$term_count = $search_form->get_count_var( $filter_name, $child_term->slug );
    		
    		//create a new option for the term
    		$new_option = new StdClass();
    		$new_option->value = $child_term->slug;
    		$new_option->label = $child_term->name;
    		$new_option->count = $term_count;
    		
    		array_push( $child_options, $new_option );
    	}
    	
    	// replace the old options with the new ones
    	$input_object['options'] = $child_options;
    	
    	return $input_object;
    }
    
    add_filter('sf_input_object_pre', 'sf_filter_options_sub_terms', 10, 2);

    What you need to do is change the 3 variables at the top to match your own:

    $filter_name = '_sft_category';
    $taxonomy_name = 'category';
    $parent_term_id = 2

    The first $filter_name is the name of your field in our plugin, which will be – _sft_product_cat
    The second is the actual taxonomy internal name product_cat
    And the third is the WP ID of the parent Product category (you can usually figure this out from WP admin by editing the term, and checking the URL)

    Once you replace those, this code will replace the options, with the child options of $parent_term_id.

    Let me know how you get on.

    Thanks


    Trevor
    Participant

    Assuming that this is Post Categories, the easiest thing is to hide the All Categories with Custom CSS.

    .sf-field-category .sf-item-0 {
        display: none;
    }

    But, if it is a custom taxonomy, the name would be different, so you would need to substitute the class .sf-field-category with whatever the taxonomy you are using has as a class in our form.

    Is ‘This months featured blogs’ a category already in the list? If it is, you may need to re-order the list of terms. This is not so easy, but would use this filter (there are many other snippets of code using this filter in our forum):

    https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object

    Snippet search:

    https://support.searchandfilter.com/forums/search/function+sf_input_object_pre/

    But, what is not possible would be to pre-select that option on loading the form for the first time. I think that feature will arrive with V3 of our plugin in a few months time.

    #270963

    Ross
    Keymaster

    Hi Jessica

    I think I see what is happening!

    So I just tried your code (albeit changed it for my setup) and the strangest thing was happening, I was getting some checkboxes with labels missing, and some working just fine.

    I then realised (and didn’t know) that at least for checkboxes, the array of options must be in order.

    So unsetting specific values leaves “holes” in the array, that for some reason our checkbox code can’t work around for now.

    Instead I did this (push the wanted options, into a new array):

    function filter_input_object($input_object, $sfid) {
    	if( !in_array( $input_object['name'], array('_sft_pa_color') ) ) {
    		return $input_object;
    	}
    
    	if( $input_object['name'] == '_sft_pa_color' ) {
    		$new_options = array();
    		foreach($input_object['options'] as $key => $option) {
    			if ( in_array( $option->value, array( 'gray', 'green') ) ) {
    				$new_options[] = $option;
    			}
    		}
    		$input_object['options'] = $new_options;
    	}
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);

    Another way would be to keep your code exactly the way it was, but before returning $input_object do:

    $input_object['options'] = array_values( $input_object['options'] );

    This will effectively reset the indexes too.

    Let me know how you get on!

    Thanks

    #269630

    Trevor
    Participant

    You will likely need to recreate the field terms array, using this filter:

    https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object

    I am not sure if it will help, but if there are many snippets already in the forum, this forum search should find them:

    https://support.searchandfilter.com/forums/search/sf_input_object_pre+function+order/

    #267669

    In reply to: Select: Option order


    Trevor
    Participant

    You would have to do this using this filter:

    https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object

    To re-order the terms in the field.

    I am not sure if it will help, but if there are many snippets already in the forum, this forum search should find them:

    https://support.searchandfilter.com/forums/search/sf_input_object_pre+function+order/

    #266721

    Ross
    Keymaster

    Hi Boris

    So I had a little play with this and got it half working – it will be for you to tidy up.

    Basically, there is a PHP function for sorting arrays by properties – usort (in this case we want to sort the array by count) :

    
    function cmp_reorder_desc($a, $b) {
        return $a->count < $b->count;
    }
    
    function reorder_options_count( $input_object, $sfid ) {
    
    	// change _sft_level for your field name
    	if($input_object['name']!=='_sft_level') {
    		return $input_object;
    	}
    	
    	if ( ! isset( $input_object['options'] ) ) {
    		return $input_object;
    	}
    	if ( ! is_array( $input_object['options'] ) ) {
    		return $input_object;
    	}
    	
    	// $input_object['options'] // this is an array of options
    	// An option has 3 properties (and a few more):
    	//$option->label;
    	//$option->value;
    	//$option->count;
    	
    	//this doesn't take into consideration the first option "All items" - you might want to remove it (it is the first option: )
    	// $input_object['options'][0] and re-add to the array, after the sorting
    	
    	// we can use the PHP function to sort an array by "count": 
    	echo usort( $input_object['options'], "cmp_reorder_desc");
    	
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'reorder_options_count', 10, 2);

    I added some notes in for you, remember to change the field name at the beginning to your own.

    Thanks

    #264919

    Trevor
    Participant

    As this is a Taxonomy/Category/Tag field, then you will need to code a solution. You can use this filter in your child theme’s functions.php file:

    https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object

    To re-order the terms in the field.

    I am not sure if it will help, but if there are many snippets already in the forum, this forum search should find them:

    https://support.searchandfilter.com/forums/search/sf_input_object_pre+function+order/

    #262980

    Trevor
    Participant

    If it is a Post Meta field, then you can set the source to Manual and drag and drop them. If it is a Taxonomy, then you will need to code a solution. You can use this filter in your child theme’s functions.php file:

    https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object

    To re-order the terms in the field.

    I am not sure if it will help, but if there are many snippets already in the forum, this forum search should find them:

    https://support.searchandfilter.com/forums/search/sf_input_object_pre+function+order/

    #261160

    Trevor
    Participant

    There ARE some shared snippets on the forum. Try this search:

    https://support.searchandfilter.com/forums/search/sf_input_object_pre+function+order/

    If you produce something, but it doesn’t quite work, by all means share the code for us to take a look at. Custom coding doesn’t really fall within the scope of our support, but we will help where we can.

    #260342

    Trevor
    Participant

    You can use this filter in your child theme’s functions.php file:

    https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object

    To re-order the terms in that field.

    I am not sure if it will help, but if there are any snippets already in the forum, this forum search should find them:

    https://support.searchandfilter.com/forums/search/sf_input_object_pre+function+order/

Viewing 10 results - 1 through 10 (of 18 total)