Adding column to admin post list with ACF values

For this purpose we will use manage_{$post_type}_posts_columns filter and manage_{$post_type}_posts_custom_column action.

Let us explain variables that we will use further:

  • For filter and action hooks instead of {$post_type} we will use “article” word. Because my custom post type name is “article”.
  • We will look for “custom_id” custom field values.
  • “Custom ID” will be our column label.

Adding new column to admin post list.

We will do it using manage_article_posts_columns filter. Basically, “manage_article_posts_columns” filter give us array with each column settings and we merge existed array with our new. In new array as key, we must provide column name which we will be using further and column label which will be use to display in table header.

function add_custom_id_column ( $columns ) {
    return array_merge ( $columns, array (
        'custom_id' => __ ( 'Custom ID' ),
    ) );
};

add_filter( 'manage_article_posts_columns', 'add_custom_id_column' );

Notice: If you need to filter WP default post type columns, you should use manage_posts_columns filter

Filling column with data

To do it we will use manage_article_posts_custom_column action. Before outputting values, we must ensure that column name provided to function is same as we defined before. It is very important to check especially when we adding multiple columns. For this purpose we will use switch function and if column names match, function will output values received from get_field() function.

function fill_custom_column ( $column, $post_id ) {
    switch ( $column ) {

        case 'custom_id':
            echo get_field('custom_id', $post_id);
            break;

    }
}
add_action ( 'manage_article_posts_custom_column', 'fill_custom_column', 10, 2 );

Notice: If you need to fill WP default post type columns, you should use manage_posts_custom_column action.