Forums Forums Search & Filter Pro Create “top 10” lists of CPTs grouped by ACF field value

  • This topic has 13 replies, 3 voices, and was last updated 4 years ago by Ross.
Viewing 4 posts - 11 through 14 (of 14 total)
  • Anonymous
    #265734

    Hi Trevoer,
    thanks for that. I will wait for an update from you or Ross.

    Regards,
    Boris

    Ross Moderator
    #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

    Anonymous
    #266003

    Hi Ross,
    I’m okay editing PHP, but not writing it myself. If you can give me some pointers, that would be very helpful, thanks!!

    Ross Moderator
    #266721

    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

Viewing 4 posts - 11 through 14 (of 14 total)