Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

Code

// 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 add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.