How to show featured image in custom post type dashboard post page

I have added this code to my theme to add a new colmum for showing the featured image in the dashboard post page colmum. It’s working in wordpress default post page, but not in custom post type (my custom post name is photo_gallery).

add_filter('manage_posts_columns', 'posts_columns', 5);
add_action('manage_posts_custom_column', 'posts_custom_columns', 5, 3);
function posts_columns($defaults){
    $defaults['riv_post_thumbs'] = __('Thumbnail');
    return $defaults;
}
function posts_custom_columns($column_name, $id){
   if($column_name === 'riv_post_thumbs'){
        echo the_post_thumbnail( array('292, 292') );
    }
}

How can I show this custom column as first in my custom post type overview?

Related posts

1 comment

  1. I’m not sure what exactly does not work for your custom post type. There are two possible cases:

    • Your custom post type photo_gallery is hierarchical. That would mean, neither (column head and values) are shown.
    • Your post type is not hierarchical but does not support featured images.

    Anyway, if you want to add the thumbnail only to your custom post type this code should do it:

    add_filter( 'manage_photo_gallery_posts_columns', 'wpse_135433_posts_columns' );
    add_action( 'manage_photo_gallery_posts_custom_column', 'wpse_135433_posts_custom_columns', 10, 2 );
    
    function wpse_135433_posts_columns( $defaults ){
    
        $defaults = array_merge(
            array( 'riv_post_thumbs' => __( 'Thumbnail' ) ),
            $defaults
        );
    
        return $defaults;
    }
    
    function wpse_135433_posts_custom_columns( $column_name, $id ) {
    
       if ( $column_name === 'riv_post_thumbs' ) {
            echo the_post_thumbnail( array('292, 292') );
        }
    }
    

    By the way, you should use prefixes on your custom functions to avoid collisions. (In this example I used the prefix wpse_135433_.

    The function wpse_135433_posts_columns() attaches the column at the first position to the list of columns $defaults.

    If you want to use this functionality on more than your custom post type you should use

    add_filter( 'manage_posts_columns', 'wpse_135433_posts_columns', 10, 2 );
    add_action( 'manage_posts_custom_column', 'wpse_135433_posts_custom_columns', 10, 2 );
    

    for non hierarchical post types and

    add_filter( 'manage_pages_columns', 'wpse_135433_posts_columns', 10, 2 );
    add_action( 'manage_pages_custom_column', 'wpse_135433_posts_custom_columns', 10, 2 );
    

    for hierarchical post types. The second parameter passed to the manage_posts_columns filter is the current post type.

Comments are closed.