Forums › Forums › Search & Filter Pro › Custom order of post meta options
Tagged: options
- This topic has 11 replies, 3 voices, and was last updated 6 years, 11 months ago by
Ross.
-
Anonymous(Private) December 8, 2018 at 2:41 am #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(Private) December 10, 2018 at 11:01 am #195677Are you using this within our Filter Input Object filter?
https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object
Anonymous(Private) December 10, 2018 at 2:21 pm #195703Yes. 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(Private) December 10, 2018 at 4:56 pm #195747That 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:Anonymous(Private) December 10, 2018 at 5:36 pm #195760No 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
Anonymous(Private) December 11, 2018 at 5:55 am #195804Ok 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.
-
AuthorPosts