Skip Navigation

[Resolved] Ordering a Select List in Alphabetical Order on the Post Type/Edit Screen

This thread is resolved. Here is a description of the problem and solution.

Problem:

The customer asked how the 'wpt_field_options' filter can be used to dynamically generate a long and alphabetically ordered list of options for a select type field.

Solution:

Shared some details and a code snippet example to show how this filter can be used to generate options from a custom post type:


add_filter( 'wpt_field_options', 'func_to_dynamically_populate_shop_locations', 10, 3);
function func_to_dynamically_populate_shop_locations( $options, $title, $type ){
    switch( $title ){
        case 'Book Shop Locations':
            $options = array();
   
            // add first empty value
            $options[] = array('#value' => '', '#title' => '---');
   
            // get all tmna-location post items
            $args = array( 'post_type' => 'shop-location', 'posts_per_page' => -1, 'post_status' => 'publish','orderby' => 'title' );
   
            $results = get_posts( $args );
            if ( $results ) {
                foreach ( $results as $post ) {
                    $options[] = array('#value' => $post->ID, '#title' => $post->post_title);
                }
            }
   
        break;
    }
    return $options;
}

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/types-api-filters/#wpt_field_options

This support ticket is created 3 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 6 replies, has 2 voices.

Last updated by chantalM 3 years, 7 months ago.

Assisted by: Waqar.

Author
Posts
#2222357

Tell us what you are trying to do?

I have the same issue as this person: https://toolset.com/forums/topic/ordering-a-select-list-in-alphabetical-order/

I have 100+ options added to a Post Field Group as Options to select, and I may need to add more as time goes on. I would like these to be displayed in alphabetical order when selecting on the post type/edit screen...

Is there custom code that can do this? Or would it be possible to develop a sorting feature so that those who have lots of options are able to drag and drop them into place?

Failing that, Minesh mentioned in the posts above the following solution:

If you have list of your [options] stored as a post type then you can populate the custom select field options dynamically using the Toolset Types hook: wpt_field_options
=> https://toolset.com/documentation/programmer-reference/types-api-filters/#wpt_field_options

I looked at the documentation but I don't understand this.. Would I create a CPT with the list of options (each post title is the option heading) and using custom code this would populate the option select fields?

I would be grateful if you could explain this to me, but to be honest if it was possible to do the following I asked about (repeated below) this would solve all my problems! 🙂

Is there custom code that automatically display list in Alphabetical Order on post type/edit screen
Or would it be possible to develop a sorting feature so that those who have lots of options are able to drag and drop them into place in the Post Field Group screen?

#2222973

Hi,

Thank you for contacting us and I'd be happy to assist.

As Minesh shared in the other ticket ( ref: https://toolset.com/forums/topic/ordering-a-select-list-in-alphabetical-order/#post-1654075 ) the 'wpt_field_options' filter can be used to dynamically generate the options for a select type custom field:
https://toolset.com/documentation/programmer-reference/types-api-filters/#wpt_field_options

Important note: As these options are not registered in the field's settings and are inserted on the fly, you'll not be able to use this field in the front-end search through views. So this approach should only be used for field, which is needed only for storing custom field data and not to be used in the front-end search.

Suppose, you have a CPT named 'Book Shops' and it has a select type field with the title 'Book Shop Locations'. Instead of adding each location option in the field's settings manually, you'll leave the options empty in the field group.

Next, you'll create a new CPT 'Shop Locations' ( slug: 'shop-location' ) and add each location as a new post in this post type, where the location's name will be set in the post's title.

The custom function attached to the 'wpt_field_options' filter, would look like this:


add_filter( 'wpt_field_options', 'func_to_dynamically_populate_shop_locations', 10, 3);
function func_to_dynamically_populate_shop_locations( $options, $title, $type ){
    switch( $title ){
        case 'Book Shop Locations':
            $options = array();
  
            // add first empty value
            $options[] = array('#value' => '', '#title' => '---');
  
            // get all tmna-location post items
            $args = array( 'post_type' => 'shop-location', 'posts_per_page' => -1, 'post_status' => 'publish','orderby' => 'title' );
  
            $results = get_posts( $args );
            if ( $results ) {
                foreach ( $results as $post ) {
                    $options[] = array('#value' => $post->ID, '#title' => $post->post_title);
                }
            }
  
        break;
    }
    return $options;
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

It will get all the available posts from the 'Shop Locations' CPT and populate them as options for the field with the title 'Book Shop Locations'. The post titles will be used for the option titles and post IDs will be used as the option values.
( the orderby' => 'title part will ensure that options are ordered by post title )

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#2224423

Thank you for your help with this and your clear instruction. I implemented this and it worked, but I noticed when I selected an option, it would display on the front-end (as I intended via a View) but it would not display in the back-end so I can't see the option I have selected in the back-end - is this correct?

If I press Quick-Edit I can see the option I selected but when I press Update it doesn't display... is there a way round this?

#2224903

Thanks for the update and glad that it worked.

When the options of a field are being populated using the 'wpt_field_options' filter, they show properly for the field on the same post's edit screen, but not if that post is being viewed inside the relationship section, on another related posts' edit screen.

If that is what you meant then this is a known limitation and I'm afraid, there is no way around this, other than editing the post from its own edit screen.

#2224923

Thank you. One last question please - at the moment the list displays in Descending order - what do I need to change in the code for the list to display in Ascending order? (A-Z) 🙂

#2226847

To make the list show in ascending order, you can replace the following line in the code snippet:


$args = array( 'post_type' => 'shop-location', 'posts_per_page' => -1, 'post_status' => 'publish', 'orderby' => 'title' );

With:


$args = array( 'post_type' => 'shop-location', 'posts_per_page' => -1, 'post_status' => 'publish', 'orderby' => 'title', 'order' => 'ASC' );

#2229645

My issue is resolved now. Thank you!