Sortable custom column using ACF Pro select field in WordPress admin for post list

Solution:

It appears we ran across the same guides while trying to accomplish this sort of task.

This is what ended up working for me.

In my case, I wanted to add an ACF field as a column in the admin dashboard and then make that column sortable.

“directory” is the post type. “email” is the ACF name

add the column

add_filter('manage_directory_posts_columns', 'filter_directory_custom_columns');

function filter_directory_custom_columns($columns) {
    $columns['email'] = 'Email';
    return $columns;
}

fill the rows with the post data

add_action('manage_directory_posts_custom_column',  'action_directory_custom_columns');

function action_directory_custom_columns($column) {
    global $post;
    if($column == 'email') {
        $directoryfields = get_fields($post->ID);
        echo $directoryfields['email'];
    }
}

make the column sortable

add_filter( 'manage_edit-directory_sortable_columns', 'sortable_directory_custom_columns' );

function sortable_directory_custom_columns( $columns ) {
    $columns['email'] = 'email';
    return $columns;
}

This was the part I was missing.

add_action( 'pre_get_posts', 'directory_orderby' );
function directory_orderby( $query ) {
    if( ! is_admin() )
        return;
    $orderby = $query->get( 'orderby');
    if( 'email' == $orderby ) {
        $query->set('meta_key','email');
        $query->set('orderby','meta_value');
    }
}

shoutout to the UtahWP(jazzsequence, ninnypants) community for helping me work through this