Custom edit post column – category not showing

The question:

I added a custom column to a custom post type:

add_filter('manage_posts_columns', 'custom_columns', 10);  
add_action('manage_posts_custom_column', 'custom_columns_thumb', 10, 2);  

function custom_columns($columns) {         
    $columns = array(
        'cb' => '<input type="checkbox" />',
        'title' => 'Title',
        'categories' => 'Categories', // not showing
        'thumb' => __('Thumb'),
        'date' => __( 'Date' )
    );
    return $columns;
}  

function custom_columns_thumb($column_name, $id){  
    if($column_name === 'thumb') {  
        echo the_post_thumbnail( 'thumb' );  
    }  
}     

The custom column “thumb” shows properly but the category is no longer displayed. Please see image.

enter image description here

What is causing this? The categories show if I remove the custom columns.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Here I tested this code and its working fine and steps here.

  1. I am just creating a dummy custom_post_type here book with the following code.

     function kv_custom_post_books() {
       $args = array(
        'public' => true,
        'label'  => 'Books',
        'taxonomies' => array('category', 'post_tag') , 
        'supports'           => array( 'title', 'editor', 'thumbnail' )
      );
     register_post_type( 'book', $args );
    }
    add_action( 'init', 'kv_custom_post_books' );
    

Here i am not sure, you used this line. 'taxonomies' => array('category', 'post_tag') . This one gets you the default categoires to your custom post type.

  1. Now we will rewrite your action hook here. and we use the same functions no change in it.

    add_filter('manage_edit-book_columns', 'custom_columns', 10);  
    add_action('manage_posts_custom_column', 'custom_columns_thumb', 10, 2);  
    
    function custom_columns($columns) {         
       $columns = array(
          'cb' => '<input type="checkbox" />',
          'title' => 'Title',
          'categories' => 'Categories', // not showing
          'thumb' => __('Thumb'),
          'date' => __( 'Date' )
       );
      return $columns;
    }  
    
    function custom_columns_thumb($column_name, $id){  
      if($column_name === 'thumb') {  
          echo the_post_thumbnail( 'thumb' );  
      }  
    }
    

Note : I just edited only one line in your code. add_filter('manage_edit-book_columns', 'custom_columns', 10);. We have to specify your custom post type in your action hook. This is the ultimate thing here . manage_edit-book_columns Instead of default one we have to specify the custom post type name here.

Here I attached a screenshot for you,

enter image description here

Method 2

Try adding the Thumb Column instead of re-defining the whole columns.

function custom_columns($columns) {  
    return array_merge( $columns, 
        array( 'thumb' => __('Thumb', 'mytextdomain' ),
        ) );
}   

Also, make sure if you accidentally unset the Categories column when you removed the Tags, Author and Comments columns.

EDIT
After seeing the little search button label at the top right corner of the second screenshot, I can see that you are indeed using a Custom Post Type. In that case, if you want associate the default “Categories” to your CPT, use this piece of code with the current array of arguments that registers your CPT:

'taxonomies' => array('category');

Also, change your filters to manage_post_type_posts_columns and manage_post_type_posts_columns. Where post_type is your CPT.

Thanks to cybmeta and PieterGoosen for the comments.

Method 3

If you want to add new columns without breaking the bank, I find storing the old values first helps to make sure you keep what is there and only add what you need. If you want to see less then adjust with screen options.

$post_type = 'posts';
add_filter("manage_${post_type}_posts_columns", 'posts_set_custom_columns', 10);
add_action("manage_${post_type}_posts_custom_column", 'posts_render_custom_columns', 10, 2);

function posts_set_custom_columns ($columns) {

    // save existing settings for columns

    $before = array(
        'cb' => $columns ['cb'],
        'title' => $columns ['title'],
        'categories' => $columns ['categories'],
    );

    // remove all the items before our new columns

    foreach ($before as $inx => $label) {
        unset($columns[$inx]);
    }

    // push our new columns to the front

    $columns = array_merge(
        array(
            'thumb' => __('Thumb'),
    ), $columns);

    // put the first items back in the front

    $columns = array_merge($before, $columns);

    return $columns;
}

function posts_render_custom_columns ($column_name, $id) {
    if($column_name === 'thumb') {

        $size = 'thumbnail';

        // display the image or a mark to let us know it's missing
        if ( has_post_thumbnail( $id ))
            the_post_thumbnail( $size );
         else
             echo "-";
    }
}

Method 4

slider to change your post name and
slider_category to change your taxonomy category name

    <?php
/** Manage column Function */
add_filter("manage_edit-slider_columns", "slider_edit_columns");
add_action("manage_posts_custom_column", "slider_custom_columns");

function slider_edit_columns($columns) {
    $columns = array(
        "cb" => "<input type="checkbox" />",
        "title" => "Title",
        "slider_category" => "Categories",  
        "date" => "Date",
    );
    return $columns;
}

function slider_custom_columns($column) {
    global $post;
    switch ($column) {
       case 'thumb':
             echo the_post_thumbnail( 'thumb' );  
        break;
        case "slider_category":  
            echo $cat = strip_tags(get_the_term_list($post->ID, 'slider_category', '', ', ',''));  
        break;  
    default:
      break;
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment