Hi Boris
So I had a little play with this and got it half working – it will be for you to tidy up.
Basically, there is a PHP function for sorting arrays by properties – usort
(in this case we want to sort the array by count
) :
function cmp_reorder_desc($a, $b) {
return $a->count < $b->count;
}
function reorder_options_count( $input_object, $sfid ) {
// change _sft_level for your field name
if($input_object['name']!=='_sft_level') {
return $input_object;
}
if ( ! isset( $input_object['options'] ) ) {
return $input_object;
}
if ( ! is_array( $input_object['options'] ) ) {
return $input_object;
}
// $input_object['options'] // this is an array of options
// An option has 3 properties (and a few more):
//$option->label;
//$option->value;
//$option->count;
//this doesn't take into consideration the first option "All items" - you might want to remove it (it is the first option: )
// $input_object['options'][0] and re-add to the array, after the sorting
// we can use the PHP function to sort an array by "count":
echo usort( $input_object['options'], "cmp_reorder_desc");
return $input_object;
}
add_filter('sf_input_object_pre', 'reorder_options_count', 10, 2);
I added some notes in for you, remember to change the field name at the beginning to your own.
Thanks