Forums Forums Search & Filter Pro Custom order of post meta options

Tagged: 

  • This topic has 11 replies, 3 voices, and was last updated 6 years ago by Ross.
Viewing 10 posts - 1 through 10 (of 12 total)
  • Anonymous
    #195630

    Is there a way to give priority to specific values of post meta in the choice drop down? I need to prioritize specific values to be at the top and the rest can be alphabetical.

    If that is not possible, is there a way to order the choice drop down options by the number of posts that match? So the highest numbers are listed first?

    Anonymous
    #195635

    Ok so I found a a script you have put together to filter the options a bit. However, I am having trouble accessing the specific values I want to move around. For example, if I have a select field named “colors” and have an array array('blue', 'green', 'yellow') but I want ‘green’ to be listed at the top, how do I access that value? I have tried the following with no luck:

    $input_object['options']->value="green';

    foreach ($input_object['options'] as $option) {
      array_unshift($input_object['options'], $option->value='green'); // returns blank
    }

    So how do I specify a certain value that I want to move? Thanks!

    Trevor
    #195677

    Are you using this within our Filter Input Object filter?

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

    Anonymous
    #195703

    Yes. So for example, I am using:

    function filter_input_object($input_object, $sfid) {
     
      if(($input_object['name']!='_sfm_colors')||($input_object['type']!='select')) {
       return $input_object;
      }
     	
      if(!isset($input_object['options'])) {
        return $input_object;
      }
     
     }
     add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);

    But when I try to access a specific option, I have not been able to do so with success. I am not sure of the array structure which is part of the issue.

    Trevor
    #195747

    That can’t be the entire code you are using?

    That code checks to make sure you apply it only to the field, and that the field has options:

    if(!isset($input_object['options'])) {
      return $input_object;
    }

    But then you need to find which option (array position) holds ‘green’ as the value, so $input_object->value == 'green' would be the test I would think, and then move that from its current position to first after the ALL options option (which is the default first one?). The PHP manual might help:

    http://uk.php.net/manual/en/function.array-splice.php#92651

    Anonymous
    #195760

    No that’s not the entire code i was using, I was trying to show the filter and function I was using to try and access the object value. I have based my attempts on this github code.

    I have tried this:

    function filter_input_object($input_object, $sfid) {
       
     	if(($input_object['name']!='_sfm_colors')||($input_object['type']!='select')) {
     		return $input_object;
     	}
      
      if(!isset($input_object['options'])) {
    		return $input_object;
    	}
      
      foreach($input_object['options'] as $option) {
    	   $green = $option->value == 'Green';
      }
      
      array_splice( $input_object['options'], 1, 0, array($green) );
    	
    	return $input_object;
    }
    
    add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);

    It returns blank

    I just tried this:

    function filter_input_object($input_object, $sfid) {
       
     	if(($input_object['name']!='_sfm_colors')||($input_object['type']!='select')) {
     		return $input_object;
     	}
      
      if(!isset($input_object['options'])) {
    		return $input_object;
    	}
      
     $green = $input_object->value == 'Green';
      
     array_splice( $input_object['options'], 1, 0, array($green) );
    	
    	return $input_object;
    }
    
    add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);

    It also returns blank

    Trevor
    #195768

    You mean the field is blank or it has no effect?

    Anonymous
    #195780

    It enters a blank option in the drop down in the correct place. So the array_splice is working as intended, but not outputting any info.

    Trevor
    #195785
    This reply has been marked as private.
    Anonymous
    #195788

    I saw that, but I am not trying to enter a new option, just trying to move an option. Yes, since Ross wrote the Git, maybe he has some answers. I can make his Git work, I’m just trying to build on it.

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