Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress | Advanced Custom Fields | Create custom columns within admin for a custom post type using ACF
<?php
// Change the columns for the releases list screen
function change_columns( $cols ) {
$cols = array(
'cb' => '<input type="checkbox" />',
'featimg' => 'Featured Image',
'excerpt' => 'Excerpt?',
'title' => 'Title',
'artist' => 'Artist',
'catno' => 'Cat#',
'categories' => 'Category',
'tags' => 'Tags',
'date' => 'Release Date'
);
return $cols;
}
function custom_columns( $column ) {
global $post;
if( $column == 'featimg' ) {
if ( has_post_thumbnail() ) {
the_post_thumbnail( 'thumbnail' );
} else {
echo '-';
}
}
if( $column == 'excerpt' ) {
if ( has_excerpt() ) {
echo '&check;';
} else {
echo '-';
}
}
if( $column == 'artist' ) {
$artist = get_field('artist');
if( $artist ) {
echo $artist;
} else {
echo '-';
}
}
if( $column == 'catno' ) {
$catno = get_field('cat_number');
if( $catno ) {
echo $catno;
} else {
echo '-';
}
}
}
add_action( "manage_releases_posts_custom_column", "custom_columns", 10, 2 );
add_filter( "manage_releases_posts_columns", "change_columns" );
@morgyface

This comment has been minimized.

Copy link
Owner Author

@morgyface morgyface commented Jul 12, 2016

Custom columns in the post list admin screen

In this example I've created new columns for a custom-post-type called "releases". This is referenced in add_action and add_filter, you'd need to change the name within, matching the format so the add_action would become manage_POSTTYPE_posts_custom_column and the add_filter would become manage_POSTTYPE_posts_columns, replacing POSTTYPE for the name of your custom-post-type.

This code goes in your theme's functions.php file.

Another note; you'll notice I've only had to specify content for four of the nine columns, this is because they're the only non "Built-in" column types. The full list of Built-in Column Types is as follows:

  • cb
  • title
  • author
  • categories
  • tags
  • comments
  • date

I hope this helps someone. Have a magical day filled with wild monkeys and donkeys dressed as unicorns.

@bolando

This comment has been minimized.

Copy link

@bolando bolando commented Feb 8, 2021

Great, it works! I just share my shorter code for only one extra custom field called "type", if it helps somebody:

  $cols = array(
  'cb'         => '<input type="checkbox" />',
  'title'      => 'Title',
  'author'      => 'Author',
  'date'       => 'Date',
  'type'       => 'Type',
  );
  return $cols;
}
function custom_columns( $column ) {
global $post;
	if( $column == 'type' ) {
	    $typ = get_field('type1');
	    if( $typ ) {
	        echo $typ;
		} else {
			echo '-';
		}
	}
}
add_action( "manage_job_posts_custom_column", "custom_columns", 10, 2 );
add_filter( "manage_job_posts_columns", "change_columns" );
@morgyface

This comment has been minimized.

Copy link
Owner Author

@morgyface morgyface commented Feb 8, 2021

Thanks @bolando!

@Jaklik

This comment has been minimized.

Copy link

@Jaklik Jaklik commented Apr 13, 2021

Works perfectly! Great job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment