Home › Forums › General Issues › Sort ASC a select field in backend
Hello,
Is there a way to sort ASC a “select field” called in the edit post ?
I have to add some values but it doesn’t render alphabetically.
Thank you for your help !
You can use either an acf/load_field or an acf/prepare_field filter. Using the former will cause a permanent change to the field when/if you edit it. Using the latter will only alter the order when an editor is using the field.
The values of a select field are located in $field['choices']
and is an array of field value => label pairs as shown in this guide https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
You can use this function to short the values of the choices array https://www.php.net/manual/en/function.asort.php
Hello John,
Thanks for your answer. Unfortunately, I’m not a very good developer.
I couldn’t find a solution on the Internet.
Thanks anyway.
Best regards
Add the filter like shown in the document I provided, in your filter you just have to do
$choices = $field['choices'];
asort($choices, SORT_NATURAL);
$field['choices'] = $choices;
Hello John,
Thank you for your help.
For those facing this problem, here is my code:
function my_acf_prepare_field( $field ) {
$choices = $field['choices'];
asort($choices, SORT_NATURAL);
$field['choices'] = $choices;
return $field;
}
// Apply to fields named "example_field".
add_filter('acf/prepare_field/name=your_field', 'my_acf_prepare_field');
To alphabetically sort choices in an ACF “Select” field:
Edit the field group in the WordPress dashboard.
Locate the “Select” field and manually reorder choices or use the “Order Choices” button for alphabetical sorting.
Save the field group.
Choices in the “Select” field will now appear in the specified order when editing or adding posts.
Always backup data before making changes, and refer to the latest ACF documentation for any updates.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.