# Sortable Admin Column From an ACF Field

> Adds a column showing an ACF field to a post type's admin list and lets you sort the list by it, for values like price, date or priority.

- Type: PHP
- Runs: Admin only
- Category: [Custom Post Types & Fields](https://wpide.com/library/category/post-types/)
- Tags: acf, admin, columns
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/acf-sortable-admin-column/

## Code

```php
// Post type, field name, column label and how to sort.
$wpide_acf_col = array(
    'post_type' => 'post',
    'field'     => 'price',
    'label'     => 'Price',
    'orderby'   => 'meta_value_num',
);

add_filter( "manage_{$wpide_acf_col['post_type']}_posts_columns", function ( $columns ) use ( $wpide_acf_col ) {
    $columns[ 'acf_' . $wpide_acf_col['field'] ] = $wpide_acf_col['label'];

    return $columns;
} );

add_action( "manage_{$wpide_acf_col['post_type']}_posts_custom_column", function ( $column, $post_id ) use ( $wpide_acf_col ) {
    if ( 'acf_' . $wpide_acf_col['field'] !== $column || ! function_exists( 'get_field' ) ) {
        return;
    }

    $value = get_field( $wpide_acf_col['field'], $post_id );

    if ( is_array( $value ) ) {
        $value = isset( $value['label'] ) ? $value['label'] : implode( ', ', array_filter( $value, 'is_scalar' ) );
    }

    echo '' === (string) $value ? '—' : esc_html( (string) $value );
}, 10, 2 );

add_filter( "manage_edit-{$wpide_acf_col['post_type']}_sortable_columns", function ( $columns ) use ( $wpide_acf_col ) {
    $columns[ 'acf_' . $wpide_acf_col['field'] ] = 'acf_' . $wpide_acf_col['field'];

    return $columns;
} );

// Sort with a LEFT JOIN so posts without a value stay in the list (at the end).
add_filter( 'posts_clauses', function ( $clauses, $query ) use ( $wpide_acf_col ) {
    global $wpdb;

    if ( ! is_admin() || ! $query->is_main_query() || 'acf_' . $wpide_acf_col['field'] !== $query->get( 'orderby' ) ) {
        return $clauses;
    }

    $order = 'DESC' === strtoupper( (string) $query->get( 'order' ) ) ? 'DESC' : 'ASC';
    $value = 'meta_value_num' === $wpide_acf_col['orderby'] ? 'CAST(wpide_acf_sort.meta_value AS DECIMAL(20,4))' : 'wpide_acf_sort.meta_value';

    $clauses['join']   .= $wpdb->prepare( " LEFT JOIN {$wpdb->postmeta} AS wpide_acf_sort ON ( wpide_acf_sort.post_id = {$wpdb->posts}.ID AND wpide_acf_sort.meta_key = %s )", $wpide_acf_col['field'] );
    $clauses['orderby'] = "( wpide_acf_sort.meta_value IS NULL OR wpide_acf_sort.meta_value = '' ) ASC, {$value} {$order}";

    return $clauses;
}, 10, 2 );
```

## Notes

Set the post type, field name and column label. Use 'meta_value_num' for numbers and Ymd dates, 'meta_value' for text. Posts without a value are listed last.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Sortable Admin Column From an ACF Field" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
