Support Forums

The forums are closed and will be removed when we launch our new site.

Looking for support? You can access the support system via your account.

Forums Forums Search Search Results for 'sf_input_object_pre'

Viewing 10 results - 101 through 110 (of 150 total)
  • Author
    Search Results
  • #184484

    Mike Doble
    Participant

    I have a range field with number inputs. I’ve figured out how to empty the default values out using the ‘sf_input_object_pre’ filter. However, when doing this I get 0 search results upon submitting the form. Basically, I’d like for this field to be optional. How can I achieve this?


    Mitchel van Eijgen
    Participant

    @Trevor I think I am explaining it wrong. I am not looking to change to order of the posts, I want to change the order of a specific S&F filter that I created using the S&F admin tools which is a list of checkboxes.

    This is what the field in S&F looks like

    And here is the filter on the front end of my site

    With CSS I already add icons for each item in the filter and that is the correct order. Now I need to hook in to the filter and change the order so that it is the same as in my admin, but I have no idea how to do that.

    I think I need the sf_input_object_pre filter and I already have this code

    function filter_function_name($input_object, $sfid) {
    
      if($input_object['name'] == '_sfm_category') {
        $input_object['attributes']['class'] = 'my-test-class';
        return $input_object;
      }
      return $input_object;
    }
    add_filter('sf_input_object_pre', __NAMESPACE__ . '\\filter_function_name', 10, 2);

    It took me an hour to figure out how I would get this name _sfm_category from my field category. It would be great if you could edit the docs and tell there

    If you are looking for your specific $input_object['name'] open your web inspector and look for the data attribute data-sf-field-name="_sfm_YOUR-FIELD-NAME"

    #182076

    Ross
    Keymaster

    Hi Jane

    Apologies for the delay – there’s been a good few things happenening and this slipped between the cracks.

    In regards to your original problem, you were setting the second hidden date field with JS, and it was not being picked up properly on load more / pagination.

    Well, we have a filter that allows you to set various attributes / values of each input field, via PHP (which I think may be more reliable):

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

    It actually gets a bit tricky targeting the second date field, as both the fields have the same name, so I did a little test and wrote this snippet which should work (add to functions.php):

    add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);
    global $from_to_field_count;
    $from_to_field_count = 0;
    
    function filter_input_object($input_object, $sfid){
    	
    	//we need to target the second field with this name, 
    	$date_field_name = "_sfm_my_date_field";
    	
    	if( $date_field_name === $input_object['name']){
    		global $from_to_field_count;
    		$from_to_field_count++;
    		
    		if(2 === $from_to_field_count){
    			$input_object['attributes']['value'] = "29/07/2018";
    		}
    	}
    	
    	return $input_object;	
    }

    Make sure to replace _sfm_my_date_field with the name of your date field.

    Let me know how you get on!

    Thanks

    #179852

    Trevor
    Moderator

    You would need to code this yourself, but we have a filter for that:

    Filter Input Object

    It may be possible to do as you want with that. If you search our forum for sf_input_object_pre you might find some helpful snippets, plus there is a link to some snippet code on Github in that documentation):

    https://gist.github.com/rmorse/7b59b45a14b1ca179868

    #161666

    Rob Ruifrok
    Participant

    I solved the problem using the ‘sf_input_object_pre’ filter:

    add_filter('sf_input_object_pre', 'pas_filterlabels_aan', 10, 2);
    	function pas_filterlabels_aan($input_object, $sfid) {
    
    		if($input_object['name'] == '_sfm_vakdomein') {
    			foreach($input_object['options'] as $option) {
    				$term_id = $option->value;
    				if ($option->value) {
    					$term = get_term_by('id', $term_id, 'cursussoort');
    					$term_label = $term->name;
    					$option->label = $term_label.' ('.$option->count.')';
    				}
    
    			}
    			
    		}
    	
    	return $input_object;	
    	}

    Replace ‘_sfm_vakdomein’ by your input object name (is shown in the query string) and ‘cursussoort’ by your taxonomy slug.

    #160307

    Katherine Harkness
    Participant

    Thank you for your reply. Would my best bet be to insert custom classes using sf_input_object_pre then use jquery to insert html around them?

    #152293

    Jason Lawton
    Participant

    OK Here it is. I needed to show the author name by last, first, and sometimes the username by last,first. So I have a hook (which yes, you put in functions.php) and I check the form id and the field we’re outputting, and process it based on what the values are.

    If we’re looking at the author field, i basically requery the entire author list, and regenerate the list completely. You can see I exclude some authors (like admin, and some other random people i didn’t want in the list)

    If it’s the user field, i do a loop and requery the user and then adjust the info.

    I think i wrote the two if statements at different times, because the second way could probably be applied to the first. Whatever, it works.

    function sf_edit_author_field_order($input_object, $sfid)
    {
    	if( in_array( $sfid, array( 3390, 3444, 3562 ) ) && $input_object['name'] == '_sf_author' ) {
            // requery the author list
            $author_args = array(
                'exclude_admin' => true,
                'hide_empty' => true,
                'meta_key' => 'last_name',
                'orderby' => 'meta_value',
                'order' => 'ASC',
                'exclude' => array( 429, 233, 558, 7 )
            );
            $user_query = new WP_User_Query( $author_args );
            $users = $user_query->get_results();
            $new_users = array();
            $new_users[] = (object) array(
                'label' => 'All Authors',
                'attributes' => array (
                    'class' => 'sf-level-0 sf-item-0'
                ),
                'value' => '',
                'count' => 0
            );
            // go through users and get their user_nicename and display_name
            foreach( $users as $user ) {
                $new_users[] = (object) array(
                    'attributes' => array(
                        'class' => 'sf-level-0'
                    ),
                    'value' => $user->user_nicename,
                    'label' => $user->last_name . ', ' . $user->first_name,
                    'count' => 1
                );
            }
            $input_object['options'] = $new_users;
    	} else if ( $sfid == 3444 && $input_object['name'] == '_sft_userlist' ) {
            // requery the tagged user list to get their names - last, first
            // go through the options part of the object and get the user id, and then the meta info for that
            foreach ($input_object['options'] as $option) {
                // get the user id from the term meta
                $temp_term = get_term_by( 'slug', $option->value, 'userlist');
                $temp_term->meta_user_id = get_term_meta( $temp_term->term_id, 'user_id', true );
                $last_name = get_user_meta( $temp_term->meta_user_id, 'last_name', true );
                $first_name = get_user_meta( $temp_term->meta_user_id, 'first_name', true );
                // overwrite the original label
                if ( $last_name && $first_name ) {
                    $option->label = $last_name . ', ' . $first_name;
                }
            }
        }
    	
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'sf_edit_author_field_order', 10, 2);

    Trevor Holewinski
    Participant

    Hi all

    I have a client that requires a specific url structure for their pages but would like to use one common filter on those pages, although having different fields selected by default depending on the page.

    I know it would makes sense to use archive pages, but it is very probable the client will want to add content to those pages so for now I am not considering them. But I could potentially use archive pages and add taxonomy images and the taxonomy description to the templates.

    I understand that the next major release of this plugin will have a pretty permalinks option but unfortunately I can’t wait.

    I have created “category” like pages and embedded the filter and shortcode to show results on each of those pages. I have also added a filter to sf_input_object_pre to set a default state to one of the fields, based upon the page id. The posts being filtered are a custom post type, trips, and I have used ACF fields instead of taxonomies in order to avoid a ridiculous amount of custom taxonomies.

    I could continue to write code for each page but it would be hard to maintain. Also it appears that although the default option shows as selected on the initial page view, the results don’t reflect this. But I suppose this could be overcome with just a bit of javascript used to click the submit button after the page is ready.

    The other option I see is to add an action to pre_get_posts to somehow limit what is shown. I quickly investigated this but it seemed a bit confusing. I limited the filter with this code:

    if($query->post_type == ‘search-filter-widget’)

    but haven’t really investigated further since it wasn’t obvious what needed to be changed.

    What path would you suggest I take? I have only added a couple ‘trips’ so I can easily change direction at this point.

    Support topiscs such as this one: https://support.searchandfilter.com/forums/topic/default-category-on-radio-selection/
    and this one:
    https://support.searchandfilter.com/forums/topic/rewrite-issue/

    were a great help but I get the idea there might be quicker solution. Any help you could give me would be greatly appreciated. I suppose the answer I am looking for is somewhere on these forums I just haven’t been able to find it !

    thanks
    Ken

    #141135

    abcmkt
    Participant

    Do you mean to open the url

    https://support.searchandfilter.com/forums/search/sf_input_object_pre/
    instead of
    https://support.searchandfilter.com/forums/search/sf_input_object_pre+range/ ?

    I’m afraid i’ll need some more help Trevor, in any case I really appreciate your fast answers…

    #141127

    Trevor
    Moderator

    Are these meta keys being searched with a Range field, because that would cause the problem. At this time you can sort of work around it (by using the Edit Query Argument filter to remove the unwanted range parameters). There will be snippets other users have posted on how they did this if you search for:

    sf_input_object_pre range

Viewing 10 results - 101 through 110 (of 150 total)