Add ACF Fields to Admin Columns

chris brignola 20871

If you’re using Advanced Custom Fields to add meta fields to your post type or page content, it can often be helpful to display your meta field values in the dashboard list view for your standard or custom post type. This is a simple approach that applies not just to meta fields created using ACF but to fields created using any method.

Let’s say that you have created a post type, ‘exhibition’, and two custom meta fields, ‘start_date’ and ‘end_date’. You’d like to add both meta fields to the custom post type list view:

2016-01-05_1516

You need to add the following snippets to your theme or plugin:

/*
 * Add columns to exhibition post list
 */
 function add_acf_columns ( $columns ) {
   return array_merge ( $columns, array ( 
     'start_date' => __ ( 'Starts' ),
     'end_date'   => __ ( 'Ends' ) 
   ) );
 }
 add_filter ( 'manage_exhibition_posts_columns', 'add_acf_columns' );

This filter adds your additional columns to the list. We have created an array containing two items – one for the start date and one for the end date – and merged it with the existing columns. The filter is hooked to the specific post type, in this case manage_exhibition_posts_columns, based on the format manage_POSTTYPE_posts_columns. You’ll need to edit this filter to match your custom post type slug.

Secondly, add the following code to output the meta field values:

 /*
 * Add columns to exhibition post list
 */
 function exhibition_custom_column ( $column, $post_id ) {
   switch ( $column ) {
     case 'start_date':
       echo get_post_meta ( $post_id, 'start_date', true );
       break;
     case 'end_date':
       echo get_post_meta ( $post_id, 'end_date', true );
       break;
   }
 }
 add_action ( 'manage_exhibition_posts_custom_column', 'exhibition_custom_column', 10, 2 );

Again, notice how the action hook is specific to your post type, in this case manage_exhibition_posts_custom_column. The function looks for the name of your custom columns then echoes the meta data.

Further reading

http://codex.wordpress.org/Plugin_API/Action_Reference/manage_pages_custom_column
https://codex.wordpress.org/Plugin_API/Filter_Reference/manage_posts_columns

11 comments

  1. User image

    Thank you. This was much more succinct and easy to accomplish than I’d thought. Not sure why this had been a mystery to me. Well explained. The one part that tripped me up was your inclusion of ‘public’ function — I did, in fact, copy your code and change it, but I wasn’t putting it into a class. Got it sorted though.

    Very helpful!

  2. User image

    To get sortable columns I used this little snippet.

    function my_column_register_sortable( $columns )
    {
    $columns[‘start_date’] = ‘start_date’;
    return $columns;
    }
    add_filter(“manage_edit-exhibition_sortable_columns”, “my_column_register_sortable” );

    • User image

      That’s great. You can also just hook into the same function you used to define the columns earlier if you want to use the same column set:

      add_filter( ‘manage_exhibition_posts_columns’, ‘exhibition_filter_posts_columns’ );
      add_filter( ‘manage_edit-exhibition_sortable_columns’ , ‘exhibition_filter_posts_columns’ );

  3. User image

    Gareth you are brilliant,
    I have tried quite a few tutorials without success and your tutorial was the easiest to follow and you explained it in a few sentances whereas the other tutorials were hard to follow without an explanation.

    I have achieved what I set out to do and I wish I found yours first.

    Thank you.

  4. User image

    Thank you for this! I have an unfortunate situation in which the field my client wants to show in the columns is just called ‘date’ and of course wordpress just uses the post date. I found and tested a plugin called admincolumns that will allow me to show it, but I’m not sure they’re doing it… so if there’s a way that I can do it without a plugin, I’d prefer that…

Leave a Reply

Your email address will not be published. All fields are required.