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 2 posts - 11 through 12 (of 12 total)
  • Anonymous
    #195804

    Ok I got this working! Maybe not the best way possible but it works. Here is what I have done:

    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;
    	}
      
      // Move Green To Top
      $key = array_search('Green', array_column($input_object['options'], 'value'));
      $top = $input_object['options'][$key];
      unset($input_object['options'][$key]);
      array_unshift($input_object['options'], $top);
      
      // Move All Colors Back to Top
      $key1 = array_search('', array_column($input_object['options'], 'value'));
      $top1 = $input_object['options'][$key1];
      unset($input_object['options'][$key1]);
      array_unshift($input_object['options'], $top1);
      
      return $input_object;
    }
    
    add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);

    If there is a cleaner way I am interested, but this does work.

    Ross Moderator
    #195850

    Hi Matt

    I had a look, and yeah that’s pretty much it.

    The code for re-organising arrays in PHP is never pretty, especially if you want to put a value, but there are always a bunch of ways to do the same thing. I don’t have the code written and handy but a couple of other approaches could be:

    1) Splice the array and rejoin like you mentioned before
    2) Create a list array("red", "green", "blue") of the order you want all the elements and loop through it, adding the $input_object['options'] one by one to a new array, then replace ['options'] with the new array, in the order you wanted

    I hope that helps, but it looks like you are on the right track already.

    Thanks

Viewing 2 posts - 11 through 12 (of 12 total)