Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
Joe

Sort contents of Filter Alphabetically

I have a post type of Patients with titles formatted “Lastname, First”.I have added Filter by: Patient name on the post type page. Problem is the filter is sorted by their ID number NOT their last name alphabetically.

If this isn’t possible right now, would someone be able to help me MAKE this happen?

9 years, 5 months ago
Tobias Schutter
Developer

A full example to sort by User last name.


/**
 * Filter cac/addon/filtering/options
 *
 * @param array $options Dropdown Options, it's value and labels
 * @param object $column CPAC_Column object.
 *
 */
function my_cac_filter_label_sort_user_options( $options, $column ) {

    $custom_field = 'my_user_custom_field'; // enter your field
    $column_type = 'column-meta'; // type of field: custom field

    if ( $column_type === $column->options->type && $custom_field == $column->options->field ) {

        $unsorted = array();
        foreach ( $options as $id => $label ) {
            $userdata = get_userdata( $id );
            $unsorted[ $id ] = $userdata->last_name;
        }

        asort( $unsorted );

        $sorted = array();
        foreach ( $unsorted as $id => $label ) {
            $sorted[ $id ] = $options[ $id ];
        }

        $options = $sorted;
    }

    return $options;
}
add_filter( 'cac/addon/filtering/options', 'my_cac_filter_label_sort_user_options', 10, 2 );
9 years, 5 months ago

You must be logged in to reply to this topic.