Add ACF Field Values as Admin Columns

Display ACF field values in the WordPress admin post list table with sortable column support.

php admin
|

Display ACF field values in the WordPress admin post list table with sortable column support.

Register a custom column for a post type:

<?php
add_filter( 'manage_event_posts_columns', function ( $columns ) {
    // Insert after title column.
    $new = [];
    foreach ( $columns as $key => $label ) {
        $new[ $key ] = $label;
        if ( $key === 'title' ) {
            $new['event_date'] = 'Event Date';
            $new['city']       = 'City';
        }
    }
    return $new;
} );

Populate the column with ACF field values:

<?php
add_action( 'manage_event_posts_custom_column', function ( $column, $post_id ) {
    switch ( $column ) {
        case 'event_date':
            $date = get_field( 'event_date', $post_id );
            echo $date ? esc_html( $date ) : '—';
            break;
        case 'city':
            echo esc_html( get_field( 'city', $post_id ) ?: '—' );
            break;
    }
}, 10, 2 );

Make the column sortable:

<?php
add_filter( 'manage_edit-event_sortable_columns', function ( $columns ) {
    $columns['event_date'] = 'event_date';
    $columns['city']       = 'city';
    return $columns;
} );

add_action( 'pre_get_posts', function ( $query ) {
    if ( ! is_admin() || ! $query->is_main_query() ) {
        return;
    }

    $orderby = $query->get( 'orderby' );

    if ( $orderby === 'event_date' ) {
        $query->set( 'meta_key', 'event_date' );
        $query->set( 'orderby', 'meta_value' );
    }

    if ( $orderby === 'city' ) {
        $query->set( 'meta_key', 'city' );
        $query->set( 'orderby', 'meta_value' );
    }
} );

Replace event with your custom post type slug. For the default post type, use manage_posts_columns, manage_posts_custom_column, and manage_edit-post_sortable_columns.

Stay Updated

Get ACF tips and new extensions in your inbox

No spam. Unsubscribe anytime.