Forums Forums Search & Filter Pro Set Default Radio box checked to different category

Viewing 10 posts - 1 through 10 (of 10 total)
  • Trevor
    #203245

    I understand what you need, and it has been asked for before. It is possible to set the default, but it does not alter the results shown until a search is done. V3 should have the option of setting the default and it affecting the results.

    The problem with setting the default is that the same code (it has to be done either with JavaScript post page load, or by PHP pre-form load using our filter input objects filter) tends to override any choices made by the user.

    Anonymous
    #203255
    This reply has been marked as private.
    Trevor
    #203262

    As I have no snippets to hand, I have asked Ross, the plugin developer, if he can provide you an example.

    Anonymous
    #203267
    This reply has been marked as private.
    Trevor
    #203272
    This reply has been marked as private.
    Ross Moderator
    #203304

    Hi there

    I understand the scenario.

    We have a filter, to change the default in the search form, but this won’t affect the results.

    This would be the code (untested ofc):

    function filter_input_object_categories($input_object, $sfid){
    
    	//ensure we are only filtering the correct field name - in this case the field we want to filter has the name <code>_sft_marketplace</code>
    	if($input_object['name']!='_sft_marketplace'){
    		return $input_object;
    	}
    	
    	//manually set the values shown/selected in the field - must be array even if single value
    	//this should really only occur under specific conditions, otherwise your field will be permanently set to this value
    	
    	//if the field is not already in use / selected
    	if(!isset($_GET['_sft_marketplace'])){
    		//then set it to "best wordpress app themes"
    		$input_object['defaults'] = array("best-wordpress-app-themes");
    	}
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'filter_input_object_categories', 10, 2);

    Thanks

    Anonymous
    #203490
    This reply has been marked as private.
    Ross Moderator
    #203662

    Hi there

    Do you mean the results do not reflect the new “default” we are setting?

    Thanks

    Anonymous
    #203938
    This reply has been marked as private.
    Ross Moderator
    #204275

    Ok, so what we did was simply update form field “defaults” but we didn’t update the query as well.

    So what we need to do is, under the same conditions, restrict the query to that taxonomy term:

    function filter_query_categories( $query_args, $sfid ) {
    	
    	//if search form ID = 252649, the do something with this query
    	if($sfid==252649)
    	{
    		//if the marketplace taxonomy is not already in use / selected
    		if(!isset($_GET['_sft_marketplace'])){
    			
    			//modify $query_args here before returning it
    			$query_args['tax_query'] = array(
    				'relation' => 'AND',
    				array(
    					'taxonomy' => 'marketplace',
    					'field'    => 'slug',
    					'terms'    => array("best-wordpress-app-themes") //restrict it
    				)
    			);
    		}
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_query_categories', 20, 2 );

    That should just about do it.

    Let me know how you get on.

    Thanks

Viewing 10 posts - 1 through 10 (of 10 total)