Support

Account

Home Forums Backend Issues (wp-admin) Having trouble adding ACF field to admin columns

Solved

Having trouble adding ACF field to admin columns

  • I’ve got a custom post type for which I’m trying to add a column to the admin panel. The column itself is showing up just fine, but I’m not having any luck populating it with the contents of the field itself.

    Here’s the code I’m using:

    function my_page_columns($columns)
    {
    	$columns = array(
    		'cb'	 	=> '<input type="checkbox" />',
    		'title' 	=> 'Last Name',
    		'first' 	=> 'First Name',
    		'date'		=>	'Date',
    	);
    	return $columns;
    }
    
    function my_custom_columns($column)
    {
    	global $post;
    	
    	if ($column_name == 'first') {
                    echo get_field( "first_name", $post_id );
            }
            elseif {
                 echo '';
            }
    }
    
    add_action("manage_posts_custom_column", "my_custom_columns");
    add_filter("manage_edit-post_staff_columns", "my_page_columns");

    I’m sure I’m missing something very obvious.

  • Hi @brotsky_pixie

    I believe that’s because you were using $post_id variable, which doesn’t have any value yet. Could you please change it to $post->ID instead like this:

    echo get_field( "first_name", $post->ID );

    To learn more about adding admin custom column, kindly check this page: http://www.elliotcondon.com/advanced-custom-fields-admin-custom-columns/.

    I hope this helps 🙂

  • No dice (I’d actually tried that $post->ID at one point). I’ve also already reviewed that page from Elliot Condon.

    Any other ideas?

  • Hi @brotsky_pixie

    It seems there are some changes on how to add the admin custom columns. Also, it seems you have several errors in your code. If your custom post type is “staff”, could you please try the following code?

    function my_page_columns($columns)
    {
        $columns = array(
            'cb'         => '<input type="checkbox" />',
            'title'     => 'Last Name',
            'first'     => 'First Name',
            'date'        =>    'Date',
        );
        return $columns;
    }
    
    function my_custom_columns($column)
    {
        global $post;
        
        if ($column == 'first') {
            echo get_field( "first_name", $post->ID );
        }
        else {
             echo '';
        }
    }
    
    add_action("manage_staff_posts_custom_column", "my_custom_columns");
    add_filter("manage_staff_posts_columns", "my_page_columns");

    Because this is more related to WordPress, kindly visit WordPress community for further support.

    I hope this helps 🙂

  • Those actually weren’t errors, as far as I can tell. The post type is called “post_staff”, not “staff”. So the column itself is appearing. The only problem I’m having is populating that column from ACF, which is why I thought this was an ACF issue, and not WP. All I’m needing is to get that field populated.

  • Hi @brotsky_pixie

    The error I meant was the $column_name variable you used instead of $column and the elseif logic instead of else. If your custom post type is “post_staff”, then you should be able to do it like this:

    function my_page_columns($columns)
    {
        $columns = array(
            'cb'         => '<input type="checkbox" />',
            'title'     => 'Last Name',
            'first'     => 'First Name',
            'date'        =>    'Date',
        );
        return $columns;
    }
    
    function my_custom_columns($column)
    {
        global $post;
        
        if ($column == 'first') {
            echo get_field( "first_name", $post->ID );
        }
        else {
             echo '';
        }
    }
    
    add_action("manage_post_staff_posts_custom_column", "my_custom_columns");
    add_filter("manage_post_staff_posts_columns", "my_page_columns");

    I said it’s related to WordPress because this code:

    echo get_field( "first_name", $post->ID ); should show the correct value. The only issue is how you show it.

    Also, could you please set WP_DEBUG to true to see any error messages on the page? Please take a look at this page to learn how to set it: https://codex.wordpress.org/Debugging_in_WordPress.

    I hope this makes sense 🙂

  • BAM! That code made the difference. I apologize for not having seen that error. Thank you LOADS!!! *MWUAH*

  • The Code from Elliot’s post doesn’t work for me anymore.
    This is what I used in my functions.php file

    add_action("manage_pages_custom_column", "add_columns");
    add_filter("manage_edit-page_columns", "set_column_content");
    
    function add_columns($columns)
    {
    	$columns = array(
    		'cb'	 	=> '<input type="checkbox" />',
    		'title' 	=> 'Title',
    		'test' 	        => 'Test',
    		'author'	=> 'Author',
    		'date'		=> 'Date',
    	);
    	return $columns;
    }
    
    function set_column_content($column)
    {
    	global $post;
    	if($column == 'test')
    	{
    		echo 'test';
    	}
    }

    I get the following error message:

    Warning: Invalid argument supplied for foreach() in path_to_my_wordpress_install/wp-admin/includes/class-wp-list-table.php on line 1074

    Can someone please confirm this? I don’t think this is an error due to my local setup though.

  • @marcelgro

    I was getting the same error when using the above code. Not sure what the problem is. Here’s what I’m using, which seems to work fine:

    
    add_filter('manage_events_posts_columns' , 'mysite_add_book_columns');
    
    function mysite_add_book_columns( $columns ) {
    
      $columns = array(
        'cb'           => '<input type="checkbox" />',
        'title'        => 'Title',
        'start_date'   => 'Start Date',
        'end_date'     => 'End Date',
        'location'     => 'Location',
        'categories'   => 'Categories',
        'date'         =>  'Date',
      );
      return $columns;
    }
    
    add_action( 'manage_events_posts_custom_column', 'mysite_custom_event_columns', 10, 2 );
    
    function mysite_custom_event_columns( $column ) {
    
      global $post;
    
      switch ( $column ) {
        case 'start_date':
          echo get_field( "event_start_date", $post->ID );
          break;
    
        case 'end_date':
          echo get_field( "event_end_date", $post->ID );
          break;
    
        case 'location':
          echo get_field( "location", $post->ID );
          break;
      }
    
    }
    
Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.