WordPress / Web Development Tutorials
(Best WordPress Tutorials)

CSSHTMLJavaScriptjQueryMySQLPHPSilvaTechnologiesWooCommerceWordpress
Silva Web Designs - Blog

Adding ACF Fields as Admin Columns to your CPT

We all know how awesome Advanced Custom Fields (ACF) is right? We pretty much use the Pro version of it on every WordPress website build we do.

Today, we are going to show you how to add fields to your CPT (Custom Post Type) to the backend Admin Columns.

There are a few plugins that can accomplish adding your ACF fields as admin columns to the backend. Admin Columns is one of the best ones that does the trick. The paid version allows your custom post type to connect with ACF Pro.

As programmers, we want to keep the number of plugins we use to a minimum right? well, it’s pretty simple to add these manually!

Okay, so here we go… Let’s say that you have created a post type, ‘hosting’, 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. First of all, we will need to add the following to the functions.php file:-


/**
 *	ACF Admin Columns
 *
 */

 function add_acf_columns ( $columns ) {
   return array_merge ( $columns, array ( 
     'start_date' => __ ( 'Starts' ),
     'end_date'   => __ ( 'Ends' ) 
   ) );
 }
 add_filter ( 'manage_hosting_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_hosting_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 Hosting CPT
 */
 function hosting_custom_column ( $column, $post_id ) {
   switch ( $column ) {
     case 'start_date':
       echo get_post_meta ( $post_id, 'hosting_start_date', true );
       break;
     case 'end_date':
       echo get_post_meta ( $post_id, 'hosting_end_date', true );
       break;
   }
}
add_action ( 'manage_hosting_posts_custom_column', 'hosting_custom_column', 10, 2 );

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

Awesome, we’ve added the fields now! But wait, do you want to go the extra step and make the fields sortable? Of course, why wouldn’t you! Here’s how we can do that:-


 /*
 * Add Sortable columns
 */

function my_column_register_sortable( $columns ) {
	$columns['start_date'] = 'start_date';
	$columns['end_date'] = 'start_date';
	return $columns;
}
add_filter('manage_edit-hosting_sortable_columns', 'my_column_register_sortable' );

Well, we hope you have found this tutorial usual, be sure to leave a comment if this has helped you or if you require any help!

 

Nathan da Silva - Profile

Posted by: Nathan da Silva

Nathan is the Founder of Silva Web Designs. He is passionate about web development, website design and basically anything digital-related. His main expertise is with WordPress and various other CMS frameworks. If you need responsive design, SEO, speed optimisation or anything else in the world of digital, you can contact Silva Web Designs here; [email protected]

It’s good to share

20 thoughts on “Adding ACF Fields as Admin Columns to your CPT

  1. Hi,

    Thank you for great tutorial. It’s all very clear but I am having problems with the second part. My CPT is a catalogue called ‘trading_hut_products’ and I’ve put this in everywhere that ‘hosting’ is mentioned but it doesn’t seem to be working:

    /*
    * Add columns to Trading Hut CPT
    */
    function trading_hut_products_custom_column ( $column, $post_id ) {
    switch ( $column ) {
    case ‘product_price’:
    echo get_post_meta ( $post_id, ‘trading_hut_products_product_price’, true );
    break;
    case ‘product_description’:
    echo get_post_meta ( $post_id, ‘trading_hut_products_product_description’, true );
    break;
    }
    }
    add_action ( ‘manage_trading_hut_products_posts_column’, ‘trading_hut_products_custom_column’, 10, 2 );

    The other admin column I’d like to include and sort by is a CPT taxonomy called ‘product_categories’, but not are how to include that. Any help appreciated!

  2. Thanks for explaining this. Columns show fine but no data in the columns. The data displays fine on the page. I must be doing something wrong:
    ‘function add_acf_columns ( $columns ) {
    return array_merge ( $columns, array (
    ‘webinar_date’ => __ ( ‘Webinar Date’ ),
    ‘webinar_time’ => __ ( ‘Time’ )
    ) );
    }
    add_filter ( ‘manage_webinars_posts_columns’, ‘add_acf_columns’ );

    /* Add columns to Hosting CPT */
    function webinars_custom_column ( $column, $post_id ) {
    switch ( $column ) {
    case ‘webinar_date’:
    echo get_post_meta ( $post_id, ‘webinars_webinar_date’, true );
    break;
    case ‘webinar_time’:
    echo get_post_meta ( $post_id, ‘webinars_webinar_time’, true );
    break;
    }
    }
    add_action ( ‘manage_webinars_posts_custom_column’, ‘webinars_custom_column’, 10, 2 );

    1. In the function “_custom_column” (eg, “hosting_custom_column” or “webinars_custom_column”), the field name in the “echo_post_meta” line should be only the name of the ACF field WITHOUT the custom post type prefix. So, in the original example, it would be:
      echo get_post_meta ( $post_id, ‘start_date’, true );

      and in Lucas’ example, it would be:
      echo get_post_meta ( $post_id, ‘webinar_date’, true );

  3. Thanks for a thorough functions file examples and description – I was looking to add “views” and found your page 🙂

  4. Thanks mate!! I do have this working with the below code. Is there a way to have the column sort the data alphabetically? We are trying to sort thru over 3500 posts.

    /**
    * ACF Admin Columns
    *
    */

    function add_acf_columns ( $columns ) {
    return array_merge ( $columns, array (
    ‘competitor_name’ => __ ( ‘Competitor’ )
    ) );
    }
    add_filter ( ‘manage_interchange_posts_columns’, ‘add_acf_columns’ );

    /*
    * Add columns to Interchange CPT
    */
    function interchange_custom_column ( $column, $post_id ) {
    switch ( $column ) {
    case ‘competitor_name’:
    echo get_post_meta ( $post_id, ‘competitor_name’, true );
    break;
    }
    }
    add_action ( ‘manage_interchange_posts_custom_column’, ‘interchange_custom_column’, 10, 2 );

    /*
    * Add Sortable columns
    */

    function my_column_register_sortable( $columns ) {
    $columns[‘competitor_name’] = ‘competitor_name’;
    return $columns;
    }
    add_filter(‘manage_edit-interchange_sortable_columns’, ‘my_column_register_sortable’ );

  5. One additional question. Why doesn’t the competitor name data not appear in the search function on top right of the table? Can this be added?

  6. Any thought on how to have the column data sort alphabetically and or show up in the search function results?

  7. Thank you for the snippets, worked like a charm… is there any way to keep the default “date” column as the last column? This method seems to add my new columns after the date column (to the right), and not before. Thanks so much!

  8. Thanks for the tutorial, I tested it and everything worked great.
    I would like, if possible, a tutorial explaining how to organize the columns, whether they are the custom ones, those set by plugin and the native ones of wordpress.

  9. Hi,

    Thank you for great tutorial. It’s all very clear but unfortunately post sorting doesn’t work properly. For example, if I change the “start_date” field to text and fill in the values A, B, C, D for each post, the posts are sorted, for example A, C, D, B.

    Link for sorting but looks well

    edit.php?post_type=hosting&orderby=start_date&order=asc
    edit.php?post_type=hosting&orderby=start_date&order=desc

    There is a screenshot for CPT when I aply sorting for columns START DATE
    https://drive.google.com/file/d/12uDbFQHm5pWjpal5O94oib7d0Y9JbWOe/view?usp=sharing

    Thank for your tips solve this issue

    Reagards,
    Tomas

  10. That is a super tutorial, easy to follow and implement. My question is now how to display an image from a custom field into one of this columns? Thank you.

  11. @Loic, I did this yesterday. I always prepend the name of my ACF fields with its type, so I can. identify if it’s an image:

    if ( str_contains( $meta_field, ‘image’ ) ) {
    $image_url = wp_get_original_image_url( get_post_meta( $post_id, $meta_field, true ) );
    echo “”;
    } else {
    echo get_post_meta( $post_id, $meta_field, true );
    }

Join the discussion

Related Posts

Related - ACF Repeater Load More using AJAX

Wordpress / 21st June 2021

ACF Repeater Load More using AJAX

Read More Related - Our Top 10 Used Plugins For WordPress – 2021 Edition

Wordpress / 20th December 2020

Our Top 10 Used Plugins For WordPress – 2021 Edition

Read More