2

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.

8
  • What do you expect in that "Categories" column? By default, categories (the core "Categories" taxonomy) is not supported by custom post types. Does your custom post type support the core "Categories" taxonomy or do you want some other custom taxonomy in the "Categories" column? Please, explain.
    – cybmeta
    Commented Dec 11, 2015 at 10:28
  • @cybmeta I guess the OP is trying to manipulate the core "Posts". Look at the filters he is using.
    – Abhik
    Commented Dec 11, 2015 at 10:40
  • @cybmeta category, not categories. categories can be a valid custom taxonomy Commented Dec 11, 2015 at 11:08
  • @Abhik post, not posts. posts can be a valid custom post type Commented Dec 11, 2015 at 11:08
  • @PieterGoosen the OP said: "I added a custom column to a custom post type", I don't he/she wants to manipulate the core "posts" type, or maybe yes.....who knows.
    – cybmeta
    Commented Dec 11, 2015 at 11:33

4 Answers 4

6
+75

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

5
  • 1
    According with codex, manage_edit-${post_type}_columns filter was supplanted by manage_${post_type}_posts_columns since WP 3.1, almost 5 years ago (although manage_edit-${post_type}_columns still works and it is not marked as deprecated). Also, manage_posts_columns filter, used by the OP, is absolutely valid and applies to all posts types except pages. I think the used filter is not the problem.
    – cybmeta
    Commented Dec 11, 2015 at 20:59
  • I am not answering by just reading codec. I tested it completely. Than I posted my code. Also I attached a screenshot for reference. There @Abhik edited his answer as well.
    – Kvvaradha
    Commented Dec 12, 2015 at 0:36
  • Sorry @Kvvaradha, it seems that you didn't understand my comment. I didn't say that your code doesn't work. Please, read my comment again.
    – cybmeta
    Commented Dec 12, 2015 at 6:37
  • I understand it. You are saying that there is an action supplanted for the action hook which I used. Your suggestion is right. But I preferred my desired one. That's it.
    – Kvvaradha
    Commented Dec 12, 2015 at 7:27
  • Yes, I said that and also that it is not the problem because changing one filter for the another makes no difference for the problem described in the question. Sorry if I was not good enough with the explanation. And this is the same discussion we had with Abhik, by the way.
    – cybmeta
    Commented Dec 12, 2015 at 8:07
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.

1
  • I guess you edited your answer after seeing my answer.
    – Kvvaradha
    Commented Dec 11, 2015 at 17:34
1

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 "-";
    }
}
2
  • manage_{$post_type}_columns should be "manage_{$post_type}_posts_columns and manage_{$post_type}_custom_column should be manage_{$post_type}_posts_custom_column.
    – cybmeta
    Commented Dec 11, 2015 at 22:41
  • 1
    So many? This answer has only 1 downvote (not mine by the way)
    – cybmeta
    Commented Dec 15, 2015 at 14:57
0

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;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.