Adding custom columns to an admin list table

When working with thumbnails, I want to display in the admin page and it has nothing to do with the
'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),

I have managed to do the following

add_filter( 'manage_portfolio_posts_columns', function( $columns ) {
	$columns['riv_post_thumbs'] = esc_html__( 'Thumbnail', 'luthemes' );

	return $columns;
}, 10 );

add_action( 'manage_posts_custom_column', function( $name, $id ) {
	if ( $name === 'riv_post_thumbs' ) {
		the_post_thumbnail( [ 150, 150 ] );
	}
}, 10, 2 );

do anyone know if the riv_post_thumbs is the default name.

1 Like

Yes. That part looks correct. I suspect the issue is that you’re hooking the first bit to a portfolio post type and the second bit is (almost) hooked to the core post post type. You’ll need to make a couple of changes.

manage_portfolio_posts_columns

manage_posts_custom_column

…should be as follows to hook into the core posts list:

manage_post_posts_columns

manage_post_posts_custom_column

…or as follows to hook in to a CPT list for a portfolio post type:

manage_portfolio_posts_columns

manage_portfolio_posts_custom_column

1 Like

is there a way to reorder the tabs… by default is
Title, Categories, Date and Thumbnail
i wanted to move the Thumbnail between Title and Categories

Just reorder the $columns array however you want before returning it.

add_filter( 'whatever_hook_you_are_using', function( $columns ) {
	
	// Empty the $columns var.
	$columns = [];
	
	// Recreate the $columns var how you like.
	$columns = [
		'title'            => esc_html('Title', 'luthemes'),
		'riv_post_thumbs'  => esc_html('Thumbnail', 'luthemes'),
		'categories'       => esc_html('Categories', 'luthemes'),
		'date'             => esc_html('Date', 'luthemes'),
	];

	return $columns;

}, 10 );
add_filter( 'manage_portfolio_posts_columns', function( $columns ) {
	$ordeby = [
		'title' => $columns['title'],
		'riv_post_thumbs' => esc_html__( 'Thumbnail', 'luthemes' ),
		'categories' => esc_html__( 'Categories', 'luthemes' )
	];

	return $ordeby;
}, 10, 2 );

so i change itto this, but for some reason the categories is not showing up properly… since this is the portfolio categories, the portfolio_category is the taxonomy that’s get register to the portfolio cpt.

any idea.

Just var_dump($columns) before modifying it and you can see what the values are.

rray(4) { ["title"]=> string(5) "Title" ["riv_post_thumbs"]=> string(9) "Thumbnail" ["categories"]=> string(10) "Categories" ["date"]=> string(4) "Date" } array(4) { ["title"]=> string(5) "Title" ["riv_post_thumbs"]=> string(9) "Thumbnail" ["categories"]=> string(10) "Categories" ["date"]=> string(4) "Date" } array(4) { ["title"]=> string(5) "Title" ["riv_post_thumbs"]=> string(9) "Thumbnail" ["categories"]=> string(10) "Categories" ["date"]=> string(4) "Date" } array(4) { ["title"]=> string(5) "Title" ["riv_post_thumbs"]=> string(9) "Thumbnail" ["categories"]=> string(10) "Categories" ["date"]=> string(4) "Date" } array(4) { ["title"]=> string(5) "Title" ["riv_post_thumbs"]=> string(9) "Thumbnail" ["categories"]=> string(10) "Categories" ["date"]=> string(4) "Date" } array(4) { ["title"]=> string(5) "Title" ["riv_post_thumbs"]=> string(9) "Thumbnail" ["categories"]=> string(10) "Categories" ["date"]=> string(4) "Date" } array(4) { ["title"]=> string(5) "Title" ["riv_post_thumbs"]=> string(9) "Thumbnail" ["categories"]=> string(10) "Categories" ["date"]=> string(4) "Date" } array(4) { ["title"]=> string(5) "Title" ["riv_post_thumbs"]=> string(9) "Thumbnail" ["categories"]=> string(10) "Categories" ["date"]=> string(4) "Date" } array(4) { ["title"]=> string(5) "Title" ["riv_post_thumbs"]=> string(9) "Thumbnail" ["categories"]=> string(10) "Categories" ["date"]=> string(4) "Date" } array(4) { ["title"]=> string(5) "Title" ["riv_post_thumbs"]=> string(9) "Thumbnail" ["categories"]=> string(10) "Categories" ["date"]=> string(4) "Date" } array(4) { ["title"]=> string(5) "Title" ["riv_post_thumbs"]=> string(9) "Thumbnail" ["categories"]=> string(10) "Categories" ["date"]=> string(4) "Date" }

This doesn’t help much. It looks like you added var_dump after modifying the array instead of before as suggested, and you haven’t posted the code you were using so we can’t tell if you may have made some other mistake.

Anyway, probably best to do it more like this, to keep all the columns that were already set and avoid interfering with other plugins…

$columns_new = [];
foreach ( $columns as $key => $value ) {
    $columns_new[ $key ] = $value;
    if ( $key === 'title' ) {
        // Add 'thumbnail' after 'title'
        $columns_new['thumbnail'] = __( 'Thumbnail', 'luthemes' );
    }
}
// If the 'title' column is not set then the 'thumbnail' column won't be
// added either. This is probably not worth worrying about.
return $columns_new;
1 Like

What James said. Further, you must specify to show the admin column when creating the taxonomy. Do that and you’re set.

1 Like

Ha ha, pointing to WP…
register_taxonomy() | Function | ClassicPress Documentation

2 Likes

James makes a good point about not messing with other plugins’ output. I’d do it like this:

add_filter( 'whatever_hook_you_are_using', function( $columns ) {
	
	// Not your post type? Don't change $columns.
	if (get_post_type() !== 'portfolio') {
		return $columns;
	}

	// Empty the $columns var.
	$columns = [];
	
	// Recreate the $columns var how you like.
	$columns = [
		'title'            => esc_html('Title', 'luthemes'),
		'riv_post_thumbs'  => esc_html('Thumbnail', 'luthemes'),
		'categories'       => esc_html('Categories', 'luthemes'),
		'date'             => esc_html('Date', 'luthemes'),
	];

	return $columns;

}, 10 );
1 Like

Habit. Honestly, I still use the WP reference mostly because I still do WP work. :smiley: I changed the URL.

1 Like

James solution works for me!

1 Like

This will still modify other plugins’ output if other plugins change the columns of the portfolio post type.

As long as the hook that’s being used is specific to a single post type, the post type check is not needed.

Noting that James’ solution got your taxonomy into the table. That was 3 solutions after what you actually asked.

3 Likes

yeah it can still modiy other plugins, but it works finally as i expected it… thanks.

1 Like

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.