Custom order of post meta options

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

Tagged: 

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)

Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.