johnbillion / extended-cpts

A library which provides extended functionality to WordPress custom post types and taxonomies.
GNU General Public License v2.0
979 stars 96 forks source link

Admin column with callback is not sortable #144

Closed iamcanadian1973 closed 4 years ago

iamcanadian1973 commented 4 years ago

Trying to set sortable columns for ACF fields. Maybe I'm misunderstanding what is possible?

'zip' => array(
                'title'    => 'Zip Code',
                'sortable' => true,
                'function' => function() {
                    $google_map = get_field( 'google_map' );
                    if( ! empty( $google_map['postal_code'] ) ) {
                        echo $google_map['postal_code'];
                    }
                },
            )
johnbillion commented 4 years ago

WordPress needs a database field name to sort by in order to fetch the results in the correct order. As you're storing the postal code within the Google map field as an associative array there's no way for WordPress to sort by that value.

If you're able to restructure your code so that the postal code is stored in its own meta field instead of within an array in the Google map field then you can make it sortable by treating it as a regular meta field column type.