Forums › Forums › Search & Filter Pro › Custom order of post meta options
Tagged: options
- This topic has 11 replies, 3 voices, and was last updated 5 years, 11 months ago by Ross.
-
Anonymous(Private) December 7, 2018 at 9:00 pm #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(Private) December 8, 2018 at 2:41 am #195635Ok 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
-
AuthorPosts