Forums › Forums › Search & Filter Pro › Create “top 10” lists of CPTs grouped by ACF field value
Tagged: acf, cpt, top 10 posts with value, widget
- This topic has 13 replies, 3 voices, and was last updated 4 years ago by Ross.
-
Ross Moderator(Private) November 11, 2020 at 8:33 am #265946
Hi Boris
Creating this code is the realm of custom work – are you comfortable editing php?
I should be able to give some pointers.
Best
Ross Moderator(Private) November 17, 2020 at 12:07 pm #266721Hi 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 bycount
) :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
-
AuthorPosts