Add ACF Value to Custom Post Type WP Admin List


Displaying the value of an Advanced Custom Fields (ACF) field in the WordPress admin post list can be really handy for quick overviews. You can do this by using the manage_posts_columns and manage_posts_custom_column hooks for posts or custom post types.

Here’s a basic example that shows how to display an ACF field named my_things in the admin post list for a custom post type named josh_client:

Step 1: Add a New Column

Add this code to your theme’s functions.php file to add a new column header:

function add_acf_columns($columns) {
    $columns['my_things'] = 'My Thing';
    return $columns;
}
add_filter('manage_josh_client_posts_columns', 'add_acf_columns');

This code adds a new column called ‘My Thing’ to the post list for the josh_client custom post type.

Step 2: Display ACF Field Value in the New Column

Next, populate that column with the value from the ACF field:

function my_things_column_content($column, $post_id) {
    if ( $column == 'my_things' ) {
        $my_things = get_field('my_things', $post_id);
        if ( $my_things ) {
            echo $my_things;
        } else {
            echo 'N/A';  // or whatever you'd like to display when the field is empty
        }
    }
}
add_action('manage_josh_client_posts_custom_column', 'my_things_column_content', 10, 2);

This function hooks into each custom column we’ve defined and checks to see if it’s the ‘My Thing’ column. If it is, it fetches the value of the my_things ACF field for the current post and outputs it.

Put both code snippets in your theme’s functions.php file or in a custom plugin, and the ACF field value should start showing up in the list of josh_client posts in the WordPress admin.

Remember to adjust the code according to your needs, such as different column names or ACF field names.


Use the label instead of the value from a select list.

If you’re using a Select field in ACF and you want to display the label instead of the value in the WordPress admin post list, you can do that with a bit of extra code.

ACF stores the possible choices for the Select field in the field’s settings. To get the label, you can retrieve the choices array and then find the label that corresponds to the saved value.

Here’s how to modify the my_things_column_content function to display the label:

function my_things_column_content($column, $post_id) {
    if ($column == 'my_things') {
        // Get the value
        $my_things_value = get_field('my_things', $post_id);

        // Get the field object to fetch choices
        $field_object = get_field_object('my_things', $post_id);
        $choices = $field_object['choices'];

        // Get the label
        $my_things_label = $choices[$my_things_value];

        if ($my_things_label) {
            echo $my_things_label;
        } else {
            echo 'N/A';  // Or whatever you'd like to display when the field is empty
        }
    }
}
add_action('manage_josh_client_posts_custom_column', 'my_things_column_content', 10, 2);

In this modified code snippet, get_field_object('my_things', $post_id) is used to fetch the complete field data, including the choices array. This array is used to look up the label that corresponds to the saved value ($my_things_value).

Add this modified function to your theme’s functions.php file, and it should display the label instead of the value in the WordPress admin post list for your josh_client custom post type.