Search Results for 'sf_input_object_pre'

Forums › Forums › Search › Search Results for 'sf_input_object_pre'

Viewing 10 results – 51 through 60 (of 146 total)
  • Author
    Search Results
  • #231607

    In reply to: Range overlap


    Ross
    Keymaster

    Hi Patrick

    It’s not possible within the UI, but you can use a filter to manually update a fields options (and values):
    https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object

    I’ve done a test, and here is the code you would need to affect your mileage field (copy to functions.php in your child theme):

    function update_range_field_mileage($input_object, $sfid)
    {
    	//make sure we affect the '_sfm_mileage' field only
    	if($input_object['name']=='_sfm_mileage')
    	{
    		//udpate this field before rendering
    		//all the options are stored in <code>$input_object['options']</code> as an array
    		$new_options = array();
    		
    		//create a new "default" option
    		$new_option = new StdClass();
    		$new_option->value = "";
    		$new_option->label = "KM Stand";
    		array_push($new_options, $new_option);
    		
    		//create a new range option we want to add
    		$new_option = new StdClass();
    		$new_option->value = "0+49999"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "0 - 49999"; //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 = "50000+99999"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "50000 - 99999"; //the label can be anything
    		array_push($new_options, $new_option);
    		
    		$new_option = new StdClass();
    		$new_option->value = "100000+150000"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "100000 - 150000"; //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_range_field_mileage', 10, 2);

    Let me know how you get on.

    Thanks

    #230320

    Ross
    Keymaster

    Hi Holly

    Happy new year to you 🙂

    You should be able to do this using our filter:
    https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object

    This filter will allow you to modify all of the options (as an array) before its processed, this would be the base level code:

    function sf_do_something_with_options($input_object, $sfid){
    
    	//change '_sfm_my_field_name' to the name of your field
    	if($input_object['name']=='_sfm_my_field_name'){
    	
    		//return, if this field doesn't have an options array
    		if(!isset($input_object['options'])){
    			return $input_object;
    		}
    		
    		//to modify the options, we just need to update this array:
    		//$input_object['options'];
    		//so it can be rebuilt however you want, 
    		//use var_dump($input_object['options']) to check the format of the array
    
    	}
    	
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'sf_do_something_with_options', 10, 2);

    And to see more advanced maniuplation of the options array check:
    https://gist.github.com/rmorse/7b59b45a14b1ca179868

    I hope that helps!

    #230093

    Ross
    Keymaster

    Hi Yanin

    This code should do the trick:

    
    function sf_format_date_labels($input_object, $sfid){
    
    	//change <code>_sfm_date</code> to the name of your field
    	if($input_object['name']=='_sfm_date')
    	{
    		//udpate this field before rendering
    		if(!isset($input_object['options']))
    		{
    			return $input_object;
    		}
    		
    		//now we know there are options we can loop through each one, and change what we need
    		foreach($input_object['options'] as $option)
    		{
    			if($option->value=="")
    			{//the option with no value is always the "all items" or unselected state
    				//$option->label = "This could be a default label";
    			}
    			else
    			{
    				$date = $option->value;
    				
    				//convert date to YYYY-MM-DD so strtototime doesn't mix up months / days
    				$input_date = substr($date, 0, 4) . '-' . substr($date, 4, 2). '-' . substr($date, 6, 2);
    				
    				//now format the date how we want
    				$date_format = "M j, Y";
    				$option->label = date($date_format, strtotime($input_date));
    			}
    		}
    	}
    	
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'sf_format_date_labels', 10, 2);

    Things to note:
    1) change _sfm_date to the name of your field – you can see the name of your field by checking it in the URL, after performing a search with a date value selected
    2) To change the display format, modify the line:

    
    $date_format = "M j, Y";
    

    You can format the date using the normal PHP date parameters: https://www.php.net/manual/en/function.date.php

    Let me know how you get on.

    Thanks

    #227743

    Anonymous
    Inactive

    I was able to modify the dropdown filter via a hook but it seems that the dropdown filter is not working even without using the hook in the first place.(return no result)
    Do you know why?

    fyi this is the function I used.

    function sf_input_object_pre_mod($input_object,$sfid){
      if($sfid==151){
    if($input_object['name']=='_sfm_property_bedroom'||$input_object['name']=='_sfm_property_bathroom'){
    	  $opts = $input_object['options'];
          foreach($opts as $ok=>$ov){
            $input_object['options'][$ok]->label = preg_replace('/ .+/','',$ov->label);
            $input_object['options'][$ok]->value = preg_replace('/\+.+/','+99',$ov->value);
          }
        }
      }
      return $input_object;
    }
    add_filter('sf_input_object_pre','sf_input_object_pre_mod',10,2);

    Anonymous
    Inactive

    so here is my testing filter:

    function filter_lat_long($input_object, $sfid) {	
    	if(!isset($input_object['options']))
    	{
    		return $input_object;
    	}
    	
    	if(($input_object['name']=='_sfm_location') && ($sfid=='23')) {
    		
    		foreach($input_object['options'] as $option) {
    			
    			$option->value =  'Hello';
    			
    		}
    	}	
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'filter_lat_long', 10, 2);

    however, $option->value = "Hello"; is not changing the options to “Hello” in the form. what am I missing here?


    Trevor
    Participant

    So, something like this:

    function filter_location($input_object, $sfid)
    {
    	if(($input_object['name']=='_sfm_location') && ($sfid=="23"))
    	{
    		//udpate this field before rendering
    	}
    	
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'filter_location', 10, 2);

    Trevor
    Participant

    I can’t give you a specific snippet, but there may be some on our forum. But, yes, looping through the array and matching would be the way to go.

    This search will find many:

    https://support.searchandfilter.com/forums/search/sf_input_object_pre+input_object+return/

    #224032

    Anonymous
    Inactive

    Trevor,

    Thanks for all your help. I was able to find some code and have adapted it to work the way we need. It’s below incase you need it for anyone else

    function filter_function_name($input_object, $sfid) {
    
            if ($input_object['name'] == '_sfm_attorneys') {
    
    		global $coauthors_plus;
    			
    		// Update option labels before rendering
    
    			foreach($input_object['options'] as $key => $option) {
    
    						$my_query = new WP_Query( array( 'post_type' => 'attorney', 'p' => $option->value));
    						while ($my_query->have_posts()) : $my_query->the_post();
    							$att_first = types_render_field("att_first", array());
    							$att_last = types_render_field("att_last", array());
    
    							$input_object['options'][$key]->label = $att_last . ', ' . $att_first;
    				
    						endwhile;
    
    	        	}
    
    	        	// Sort options...
    
    	        		$sortArray = array(); 
    
    	        		foreach($input_object['options'] as $option) { 
    
    				    foreach($option as $key => $value) { 
    
    				        if(!isset($sortArray[$key])) { 
    
    				            $sortArray[$key] = array(); 
    
    				        } 
    
    				        $sortArray[$key][] = $value; 
    
    				    }
    
    				}
    
    				// ...by label
    
    	        		$orderby = "label";
    
    				array_multisort($sortArray[$orderby],SORT_ASC,$input_object['options']); 
    
    	}
    		
    		// Return
    
    			return $input_object;
    
    	}
    
    	add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);
    #223973

    Anonymous
    Inactive

    Trevor,

    Thanks for the link. I’ve been able to add a function and it’s now displaying the Attorneys in the correct order but how do I get it to hide the attorneys without a post like it does by default? Right now it’s displaying all attorneys even if there’s no news associated to the attorney,

    function sf_edit_author_field_order($input_object, $sfid)
    {
    	if( $input_object['name'] == '_sfm_attorneys' ) {
            // requery the author list
            $my_query = new WP_Query( array( 'post_type' => 'attorney', 'meta_key' => 'wpcf-att_last', 'orderby' => 'meta_value', 'order' => 'ASC', 'showposts' => -1));
    		while ($my_query->have_posts()) : $my_query->the_post();
    		$att_first = types_render_field("att_first", array());
    		$att_last = types_render_field("att_last", array());
    		$theid = get_the_ID();
            // go through users and get their user_nicename and display_name
            //foreach( $my_query as $user ) {
                $new_users[] = (object) array(
                    'attributes' => array(
                        'class' => 'sf-level-0'
                    ),
                    'value' => $theid,
                    'label' => $att_last . ', ' . $att_first,
                    'count' => 1
                );
            endwhile;
            $input_object['options'] = $new_users;
    	}
    	
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'sf_edit_author_field_order', 10, 2);
    #222767

    Anonymous
    Inactive

    The Problem: I need to load an author dropdown on the filter with just the Editors

    I have a Filter for my site, with several fields, one of them is Author, but the website has more than 15.000 users registered and get a timeout error every time the field is enabled.

    I’ve tried this to re write the author field with a more manageable amount of users (only the Editors) but with no luck the idea was to remove all content from _sf_author and replace it with the editors

    `
    function filter_authors($input_object, $sfid)
    {
    if (($input_object[‘name’] != ‘_sf_author’) || ($input_object[‘type’] != ‘select’)) {
    return $input_object;
    }

    unset($input_object[‘options’]);
    $input_object[‘options’] = array();

    // Generate first array option (default)
    $first_option = new StdClass();
    $first_option->value = ”;
    $first_option->label = ‘All Authors’;

    //attributes
    $first_option->attributes = array(
    ‘title’ => ‘All Authors’,
    );

    array_push($input_object[‘options’], $first_option);

    //change classes & attributes of the field
    $input_object[‘attributes’][‘class’] = ‘user_filter’;
    $input_object[‘attributes’][‘title’] = ‘Authors’;

    //add/override prefix & postfix to the field
    $input_object[‘prefix’] = “Filter by Author”;

    //Check if options variable exists
    if (!isset($input_object[‘options’])) {
    return $input_object;
    }

    //Create new users array
    $args = array(
    ‘role’ => ‘editor’,
    ‘order’ => ‘ASC’,
    ‘orderby’ => ‘display_name’,
    );

    // Create the WP_User_Query object
    $wp_user_query = new WP_User_Query($args);
    $authors = $wp_user_query->get_results();
    foreach ($authors as $author) {
    // get all the user’s data
    $author_info = get_userdata($author->ID);
    //create new options with user values
    $new_option = new StdClass();
    $new_option->value = $author_info->user_login;
    $new_option->label = $author_info->first_name . ‘ ‘ . $author_info->last_name;

    //attributes
    $new_option->attributes = array(
    ‘title’ => $author_info->user_login,
    );
    array_push($input_object[‘options’], $new_option);
    }
    return $input_object;
    }
    // add_filter(‘sf_input_object_pre’, ‘filter_authors’, 10, 2);
    `
    Using this approach i change a meta field and filled it with the Editors data, but then the post request was send as _sfm_editors and not _sf_author and i couldn’t find a way to replace this request for the one i needed.

    Also tried building my own query accessing the search data and using the hook sf_results_url to change the url, but i cannot change the search query with it.

    I’m running out of ideas, appreciate your time to check my issue and any new ideas on how to solve it.

Viewing 10 results – 51 through 60 (of 146 total)
Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.