Home » Add New Columns To WP Posts Manage Page

WordPress Post Manage Page is the page where posts are being managed. This page list all post that can be group into Published, Draft, and Pending. You can also edit a post, trash a post, and add a new post.

Each post on the list contains basic information such as the Title, Author, Categories, and Tags spread through different columns. There are times that you would want to add a new custom column to this list that would contain an important post-related data to you.

Requirements:

  • WordPress

We will be using the custom field data from the tutorial Create Your Own WP Post Views Counter. The custom meta_key value is total_views_count, it contains the total count views of a specific post.

Therefore, the goal of this tutorial is to display the total views count of each post on a new custom column of the posts manage page.

Step 1.

Create the Column Header. This will define what data belongs to this column.

Add the following codes to functions.php file.

function new_posts_column( $columns ) {
    $columns['views']  = 'Views';
    return $columns;
}
add_filter( 'manage_posts_columns', 'new_posts_column' );

Step 2.

Get the data for the new column.

Add the following codes to functions.php file.

function new_posts_column_data( $column_name, $post_id ) {
    if ( $column_name == 'views' ) {
        $views = get_post_meta( $post_id, 'total_views_count', true );
        echo $views;
    }
}
add_action( 'manage_posts_custom_column', 'new_posts_column_data', 10, 2 );

Result.

Notes:

  • Sorting function can be added by making the column header clickable.

References:


Posted

in

by

Tags:

One response to “Add New Columns To WP Posts Manage Page”

  1. […] is a continuation of the tutorial Add New Columns To WP Posts Manage Page, thus we will be updating the function where the hook manage_posts_columns is added. And will also […]

Leave a Reply

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