Forums Forums Search Search Results for 'sf_input_object_pre function'

Viewing 10 results - 51 through 60 (of 106 total)
  • Author
    Search Results
  • #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.

    #221251

    Trevor
    Participant
    function filter_input_object($input_object, $sfid)
    {
    	if($input_object['name']!='_sf_post_type')
    	{
    		return $input_object;
    	}
    
    	if(!isset($input_object['options']))
    	{
    		return $input_object;
    	}
    	
    	foreach($input_object['options'] as $option)
    	{
    		if($option->value=="post")
    		{
    			$option->label = "News";
    		}
    	}
    	
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);

    Something like this, but in the first like change the $sfid for the actual ID number of the form. This code goes in the child theme functions.php file. I think I have it correct.


    Ross
    Keymaster

    Hi Thomas

    So I’ve found a better way to do this (that can be search form specific).

    The code in full (so remove everything else we’ve added:

    
    function filter_input_object_radio($input_object, $sfid){
    
    	if(($input_object['name']!='_sf_post_type')||($input_object['type']!='radio'))
    	{
    		return $input_object;
    	}
    	
    	if(isset($input_object['options'])){
    		//remove the first option from the array
    		$removed_option = array_shift($input_object['options']);
    	}
    	
    	//now set the default value in the field
    	//make sure we only modify a field belonging to a specific Search Form, by ID
    	if($sfid==15485){ //replace 15485 with your search form ID
    		//make sure the field is not set
    		if(!isset($_GET['post_types'])){
    			//then set it to "post"
    			$input_object['defaults'] = array("post");
    		}
    	}
    	
    	return $input_object;
    }
    
    add_filter('sf_input_object_pre', 'filter_input_object_radio', 10, 2);
    
    function set_default_post_type_query( $query_args, $sfid ) {
    	
    	//if search form ID = 225, the do something with this query
    	if($sfid==15485) //replace 15485 with your search form ID
    	{
    		//modify $query_args here before returning it
    		if(!isset($_GET['post_types'])){
    			$query_args['post_type'] = 'post';
    		}
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'set_default_post_type_query', 20, 2 );
    

    The only thing you have to replace is the search form ID of 15485 (there are 2 instances of this) – swap this out to match the ID of the search form on the homepage and it should work 🙂

    Best


    Ross
    Keymaster

    Hi Thomas

    Because we know the first option is the option we want to remove, we can do it a bit easier like this:

    function filter_input_object($input_object, $sfid){
    
    	if(($input_object['name']!='_sf_post_type')||($input_object['type']!='radio'))
    	{
    		return $input_object;
    	}
    	
    	if(isset($input_object['options'])){
    		//remove the first option from the array
    		$removed_option = array_shift($input_object['options']);
    	}	
    	
    	return $input_object;
    }
    
    add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);

    Now, I’m guessing that this will work (I didn’t test but it looks correct), however, I think next you want the results to be default fitlered by this option?

    Thanks


    Anonymous
    Inactive

    OK so this is as far as I got:

    function filter_input_object($input_object, $sfid)
    {
    
    	if(($input_object['name']!='_sf_post_type')||($input_object['type']!='radio'))
    	{
    		return $input_object;
    	}
    	
    	foreach($input_object['options'] as $option)
    	{
    		if($option->value=="")
    		{
    			
    		}
    		else if($option->value=="post")
    		{
    			
    		}
    	}
    	
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);

    All I need now is to assign actions for the ‘if’ and ‘elseif’. But I couldn’t find any example. I would like to hide or delete the option with no value and make the one with value ‘post’ preselected.

    Can you help please?

    #215959

    Ross
    Keymaster

    Hi Mark

    This would be the CSS, although how it works may vary from browser to browser:

    .searchandfilter .sf-field-category select option:first-child{
    	display: none;
    }

    This assumes that it is the field/class .sf-field-category that needs this applying to… You can inpsect the form (using your browsers dev tools) to find the class names of other fields you might want to do this to.

    However, I would recommend the PHP way using our filter to ensure consistency across browsers etc (add to functions.php):

    
    function filter_input_object($input_object, $sfid){
    	
    	if(($input_object['name']!='_sft_category'))
    	{
    		return $input_object;
    	}
    	
    	//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;
    	}
    	
    	//now we know there are options we can go ahead anre remove the firlst option
    	array_shift($input_object['options']);
    	
    	
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);
    

    In this example, I have used the same field (category), but using the URL fieldname – _sft_category.

    Thanks

    #215170

    Ross
    Keymaster

    Hi Andrew

    Your link went offline so I couldn’t see. I’m guessing you tried something like the following section of code (this adds a prefix / postfix outside the select field itself).

    Also, notice the name doesn’t have the brackets _sfm_distance:

    function filter_input_object($input_object, $sfid){
    	
    	if(($input_object['name']!='_sfm_distance')){
    		return $input_object;
    	}
    	
    	//add/override prefix & postfix to the field
    	$input_object['prefix'] = "Prefix ";
    	$input_object['postfix'] = " Postfix";
    	
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);

    However if you wanted to change the labels (or in this case append text to it) it would be like:

    function filter_input_object($input_object, $sfid){
    	
    	if(($input_object['name']!='_sfm_distance')){
    		return $input_object;
    	}
    	
    	//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;
    	}
    	
    	//now we know there are options we can loop through each one, and change what we need
    	foreach($input_object['options'] as $option){
    		$option->label .= " miles";
    	}
    	
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);

    Thanks

    PS – I’ve actaully tested both of these just now with my own field, so there shouldn’t be syntax errors.

    #215141

    Trevor
    Participant

    I had a thought. Try this:

    function add_miles_to_filter_labels($input_object, $sfid) {
      if(($input_object['name']!='_sfm_distance[]'||($sfid != 2216)) {
        return $input_object;
      }
      foreach($input_object['options'] as $option) {
        $option->label .= " miles";
      }
      return $input_object;
    }
    add_filter('sf_input_object_pre', 'add_miles_to_filter_labels', 10, 2);
Viewing 10 results - 51 through 60 (of 106 total)