Adjusting list columns in backend

I will be showing how to adjust the All Posts / All Pages / all Custom Post Type screen. To include additional columns and modify the current ones listed.

I am showing an example on adjusting the All Movies custom post type screen. To see adjustment show up in the All Movies screen.

// manage the columns of the post post type
function manage_columns_for_post($columns){
    //remove columns
    unset($columns['title']);
    unset($columns['categories']);
    unset($columns['tags']);
    unset($columns['date']);
    unset($columns['comments']);
    unset($columns['author']);
    unset($columns['taxonomy-movie_category']); // CPT
    unset($columns['taxonomy-year_made']);      // CPT

   //add new columns
    $columns['title'] = 'Title';
    $columns['post_featured_image'] = 'Featured Image';
    $columns['post_content']    = 'Content';
    $columns['taxonomy-movie_category']    = 'Movie Category'; // CPT
    $columns['taxonomy-year_made']    = 'Year Made';           // CPT
   // $columns['categories']    = 'Categories';
   // $columns['tags']    = 'Tags';
    $columns['date']    = 'Date';   
   // $columns['author']    = 'Author';
    return $columns;
}

add_action('manage_movies_posts_columns','manage_columns_for_post'); // CPT
/* All Posts screen: add_action('manage_posts_posts_columns','manage_columns_for_post'); */

The code: manage_post_posts_columns affects the post list screen.

Post columns = manage_post_posts_columns
Page columns = manage_page_posts_columns
Custom Post Type columns (movies) = manage_movies_posts_columns

I had to first unset (remove) the various columns and then add these back again in the order I wanted. There is some extra work for a Custom Post Type such I used. I had to unset (remove) the two taxonomies taxonomy-movie_category and taxonomy-year_made and then add these back in again where I wanted these seen. To find the name I went to All Movies and right click in the browser to see the name mentioned in the HTML code.

Currently the new columns are empty. Let’s add content to these.

The code used.

//Populate custom columns for post type
function populate_post_columns($column,$post_id){

   // featured image column
   if($column == 'post_featured_image'){
      //if this post has a featured image
      if(has_post_thumbnail($post_id)){
        // Original: $post_featured_image = get_the_post_thumbnail($post_id,'thumbnail');
         echo the_post_thumbnail( array(100,100) );
     }else{
        echo 'This post has no featured image'; 
     }
}


    //post content column
    if($column == 'post_content'){

       //get the page based on its post_id
       $post = get_post($post_id);
       if($post){
          //get the main content area
          $post_content = apply_filters('the_content', $post->post_content); 
          // Original: echo $post_content; 
          echo wp_trim_words(get_the_content(), 10);
       }
   }
}
add_action('manage_post_posts_custom_column','populate_post_columns',10,2);

I changed manage_post_posts_custom_column to manage_movies_posts_custom_column so that the content would be seen in the Movies custom post type.

The result of adding and reordering columns and adding in content for the custom post type Movies.

WordPress adding columns reordering and content
WordPress: adding columns, reordering and adding content.

Remember to adjust the code for your usage. Be in the All Posts Screen, All Pages Screen or a custom post type screen.

Tutorial was originally posted 15 August 2018.


Share the article:

Leave a Reply

Your email address will not be published. Required fields are marked *