I will be showing how to add and remove columns that are listed in the post, page or custom post screen in the backend.
I began by creating a new php file I named columns.php which I placed into a lib folder inside the root of the child theme.
Adjusting the post list screen columns in the backend
// 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']); //add new columns $columns['title'] = 'Title'; $columns['post_featured_image'] = 'Featured Image'; $columns['post_content'] = 'Content'; $columns['categories'] = 'Categories'; $columns['tags'] = 'Tags'; $columns['date'] = 'Date'; $columns['author'] = 'Author'; return $columns; } add_action('manage_post_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 columns = manage_movie_posts_columns
I removed (unset) all the columns and then added them back again in the order I wanted them in.
I found the names by right clicking the columns and selecting Inspect in the browser. The col id shows the names used above.
Adding content to the columns
//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);
The finished result.
Adjusting a movie custom post.
I have already made a movie custom post in this tutorial.
In the same columns.php file I added the following to affect the custom post type movie.
// manage the columns of the movie custom post type function manage_columns_for_movie($columns){ //remove columns unset($columns['title']); unset($columns['taxonomy-movie-years']); unset($columns['gadwp_stats']); unset($columns['tags']); unset($columns['taxonomy-movie-type']); unset($columns['date']); unset($columns['comments']); unset($columns['author']); //add new columns - Placing them in the order I want them in. $columns['title'] = 'Movie Title'; $columns['page_featured_image'] = 'Featured Image'; $columns['page_content'] = 'Movie Content'; $columns['taxonomy-movie-years'] = 'Year Screened '; $columns['taxonomy-movie-type'] = 'Movie Category'; $columns['date'] = 'Date'; // $columns['Author'] = 'Author'; // $columns['gadwp_stats'] = 'Stats'; - a plugin that adds a column. I removed it above and did not add it back in. // $columns['page_template'] = 'Page Template'; return $columns; } add_action('manage_movie_posts_columns','manage_columns_for_movie'); //Populate custom columns for the movie custom post type function populate_movie_columns($column,$post_id){ //featured image column if($column == 'page_featured_image'){ //if this page has a featured image if(has_post_thumbnail($post_id)){ $post_featured_image = get_the_post_thumbnail($post_id,'thumbnail'); // echo $post_featured_image; echo the_post_thumbnail( array(140,100) ); }else{ echo 'This custom post has no featured image'; } } //page content column if($column == 'page_content'){ //get the page based on its post_id $page = get_post($post_id); if($page){ //get the main content area $page_content = apply_filters('the_content', $page->post_content); echo wp_trim_words( get_the_content(), 10); } } } add_action('manage_movie_posts_custom_column','populate_movie_columns',10,2);
The finished result.
Thanks for the good content
Thanks for submitting this entry