top of page

Add Featured Image to Posts Grid/Column in Back-end for Custom Post Types [CPT]

  • Writer: سُلَيْمَان بْن دَاوُوْد
    سُلَيْمَان بْن دَاوُوْد
  • May 28, 2020
  • 1 min read

Updated: Aug 3, 2021

Here we are going to show you how to add a column of features image in Custom Post Type Grid in back end


Here is corresponding code to the workaround we created a custom post type with the slug team so hooks become like


The following hooks pattern is used

add_filter('manage_{post type slug}_posts_columns', 'callback_function_to_add_column');

add_action('manage_{post type slug}_posts_custom_column', 'callback_function_for_data', 10, 2);


The following is complete code workaround

add_filter('manage_cookie-drops_posts_columns', 'cookie_drops_custom_column');
function cookie_drops_custom_column($defaults) {
    $defaults['featured_image'] = 'Featured Image';
    $defaults['cookie_status']  = 'Cookie Status';
    $defaults['cookie_qty']          = 'Cookie Qty';
    return $defaults;
}


add_filter('manage_cookie-drops_posts_custom_column', 'cookie_drops_custom_column_data',10, 2);
function cookie_drops_custom_column_data($column_name, $post_ID){

        if ($column_name == 'featured_image') {

           $post_thumbnail_id = get_post_thumbnail_id($post_ID);
        $src = wp_get_attachment_image_src($post_thumbnail_id, 'featured_preview');
        if ($src) {
            echo '<img src="' . $src[0] . '"  height="100px"/>';
        }
    }

    if ($column_name == 'cookie_status') {
        echo get_post_meta ( $post_ID, 'cookie_status', true ); 
    }

    if ($column_name == 'cookie_qty') {
        echo get_post_meta ( $post_ID, 'stock_quantity', true ); 
    }
}








Recent Posts

See All
Hide Upcoming Events

Hiding Upcoming Events can be relatively useful when you are using ACF fields to show Dates for an an Event The Following Snippet is an...

 
 
 

Comments


© 2020 by syednazrulhassan

bottom of page