Support

Account

Home Forums Backend Issues (wp-admin) Sort checkbox choices A-Z

Solving

Sort checkbox choices A-Z

  • function sort_checkbox( $field ) {
    	
    	$field = sort($field["choices"]);
    	
        return $field;
        
    }
    
    add_filter('acf/load_field/key=field_5d4164d40898f', 'sort_checkbox');

    But it doesn’t work?

  • Hi Tringwebdesign,

    I think it’s not working because you try to save the $field[“choices”] in $field, try this :

    function sort_checkbox( $field ) {
    	$field["choices"] = sort($field["choices"]);
    	return $field;   
    }
    add_filter('acf/load_field/key=field_5d4164d40898f', 'sort_checkbox');

    I assume the sort method you use is correct, it’s may not.

  • Not working but one step closer, thank you.

  • Maybe the sort method doesn’t work … You can’t check here how to use the sort method.

    I assume you have text choices so you may have to add a sort flag (See in the link).

  • I think you should use ksort – it sorts the array by key.
    Also you can’t save it to the $field[‘choices’], because this function does it by pointers. Just do it like this:

    function sort_checkbox( $field ) {
    	ksort($field["choices"]);
    	return $field;   
    }
    
    add_filter('acf/load_field/key=field_5d4164d40898f', 'sort_checkbox');
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.