How to Add Pageviews Count Column to WordPress Posts, Pages, and Custom Post Types

Tracking pageviews is an important aspect of managing a website. In this tutorial, we will explore how to add a “Pageviews” count column to WordPress posts, pages, and custom post types. This feature allows you to keep track of the number of views each content item receives, providing valuable insights into your site’s popularity. Additionally, we will enhance the functionality by making the “Pageviews” column sortable, enabling you to sort content based on pageviews count. Please note that the pageviews count implemented in this tutorial is not unique to individual users. Let’s dive in!

Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.

Add “Pageviews” Column to Posts, Pages, and Custom Post Types

To get started, locate and open the functions.php file of your WordPress child theme. Insert the following code snippets at the end of the functions.php file:

/**
 * Add "Pageviews" Column to Posts, Pages, and Custom Post Types
 */
function add_pageviews_column($columns) {
    $columns['pageviews'] = 'Pageviews';
    return $columns;
}
add_filter('manage_posts_columns', 'add_pageviews_column');
add_filter('manage_pages_columns', 'add_pageviews_column');
add_filter('manage_{your_custom_post_type}_posts_columns', 'add_pageviews_column');

Code language: PHP (php)

This code snippet adds a “Pageviews” column to the posts, pages, and custom post types in the WordPress admin backend. It utilizes the add_filter function to modify the columns displayed in the post list tables.

Display Pageviews Count in the Column

Insert the following code snippets after the previous code:

/**
 * Display Pageviews Count in the Column
 */
function display_pageviews($column_name, $post_id) {
    if ($column_name === 'pageviews') {
        $pageviews = get_post_meta($post_id, 'pageviews', true);
        echo $pageviews ? $pageviews : '0';
    }
}
add_action('manage_posts_custom_column', 'display_pageviews', 10, 2);
add_action('manage_pages_custom_column', 'display_pageviews', 10, 2);
add_action('manage_{your_custom_post_type}_posts_custom_column', 'display_pageviews', 10, 2);

Code language: PHP (php)

This code snippet is responsible for displaying the pageviews count in the “Pageviews” column. It uses the add_action function to hook into the column rendering process and retrieve the pageviews count from the post meta.

Track and Update Pageviews

Insert the following code snippet after the previous code:

/**
 * Track and Update Pageviews
 */
function track_pageviews() {
    if (!is_user_logged_in() && is_singular()) {
        global $post;
        $pageviews = get_post_meta($post->ID, 'pageviews', true);
        $pageviews = $pageviews ? intval($pageviews) + 1 : 1;
        update_post_meta($post->ID, 'pageviews', $pageviews);
    }
}
add_action('wp_footer', 'track_pageviews');
Code language: PHP (php)

This code snippet tracks and updates the pageviews count for each post, page, or custom post type. It checks if the user is not logged in and if the current view is a singular page (single post, page, or custom post type). If these conditions are met, it retrieves the existing pageviews count, increments it by one, and updates the post meta with the new count.

Customize the Code for Custom Post Types

If you have custom post types on your website, you need to modify the code snippet. Replace {your_custom_post_type} with the slug or name of your custom post type. For example, if your custom post type is called “books,” the line would be add_filter('manage_books_posts_columns', 'add_pageviews_column'); and add_action('manage_books_posts_custom_column', 'display_pageviews', 10, 2); .

Save and Upload the Child Theme

Save the changes made to the functions.php file and upload the modified child theme to your WordPress installation. Activate the child theme to apply the code modifications.

View the Pageviews Count Column in the Backend

Once the child theme is activated, navigate to the “Posts,” “Pages,” or the relevant custom post type section in the WordPress admin backend. You will now see a new column labeled “Pageviews” displaying the corresponding pageviews count for each content item. Please note that the pageviews count represents the total number of views for each content item and is not unique to individual users.

Track Pageviews

The implemented code will automatically track pageviews and update the count for non-logged-in users when a post, page, or custom post type is viewed. However, it does not differentiate between individual users. If you require unique pageviews tracking per user, additional customization beyond the scope of this tutorial will be necessary.

Make the “Pageviews” Column Sortable

To make the “Pageviews” column sortable, add the following code snippet to your functions.php file, below the previous code:

/**
 * Make "Pageviews" Column Sortable
 */
function make_pageviews_column_sortable($columns) {
    $columns['pageviews'] = 'pageviews';
    return $columns;
}
add_filter('manage_edit-post_sortable_columns', 'make_pageviews_column_sortable');
add_filter('manage_edit-page_sortable_columns', 'make_pageviews_column_sortable');
add_filter('manage_edit-{your_custom_post_type}_sortable_columns', 'make_pageviews_column_sortable');

Code language: PHP (php)

This code snippet adds sorting capability to the “Pageviews” column. It utilizes the add_filter function to define the “Pageviews” column as sortable in the post and page list tables.

Modify Query to Sort by Pageviews

To modify the query in the admin area and sort posts, pages, and custom post types by the “Pageviews” column, add the following code snippet to your functions.php file, below the previous code:

/**
 * Modify Query to Sort by Pageviews
 */
function sort_posts_by_pageviews($query) {
    if (!is_admin() || !$query->is_main_query()) {
        return;
    }

    $orderby = $query->get('orderby');
    if ($orderby === 'pageviews') {
        $query->set('meta_key', 'pageviews');
        $query->set('orderby', 'meta_value_num');
    }
}
add_action('pre_get_posts', 'sort_posts_by_pageviews');

Code language: PHP (php)

This code snippet modifies the query in the admin area to sort posts, pages, and custom post types by the “Pageviews” column. It checks if the query is in the admin area and the main query, and if the sorting parameter is set to 'pageviews'. If so, it modifies the query to sort by the 'meta_value_num' of the 'pageviews' meta key.

Full code snippet

/**
 * Add "Pageviews" Column to Posts, Pages, and Custom Post Types
 */
function add_pageviews_column($columns) {
    $columns['pageviews'] = 'Pageviews';
    return $columns;
}
add_filter('manage_posts_columns', 'add_pageviews_column');
add_filter('manage_pages_columns', 'add_pageviews_column');
add_filter('manage_{your_custom_post_type}_posts_columns', 'add_pageviews_column');

/**
 * Make "Pageviews" Column Sortable
 */
function make_pageviews_column_sortable($columns) {
    $columns['pageviews'] = 'pageviews';
    return $columns;
}
add_filter('manage_edit-post_sortable_columns', 'make_pageviews_column_sortable');
add_filter('manage_edit-page_sortable_columns', 'make_pageviews_column_sortable');
add_filter('manage_edit-{your_custom_post_type}_sortable_columns', 'make_pageviews_column_sortable');

/**
 * Display Pageviews Count in the Column
 */
function display_pageviews($column_name, $post_id) {
    if ($column_name === 'pageviews') {
        $pageviews = get_post_meta($post_id, 'pageviews', true);
        echo $pageviews ? $pageviews : '0';
    }
}
add_action('manage_posts_custom_column', 'display_pageviews', 10, 2);
add_action('manage_pages_custom_column', 'display_pageviews', 10, 2);
add_action('manage_{your_custom_post_type}_posts_custom_column', 'display_pageviews', 10, 2);

/**
 * Modify Query to Sort by Pageviews
 */
function sort_posts_by_pageviews($query) {
    if (!is_admin() || !$query->is_main_query()) {
        return;
    }

    $orderby = $query->get('orderby');
    if ($orderby === 'pageviews') {
        $query->set('meta_key', 'pageviews');
        $query->set('orderby', 'meta_value_num');
    }
}
add_action('pre_get_posts', 'sort_posts_by_pageviews');

Code language: PHP (php)

That’s it! You have successfully added a “Pageviews” count column to your WordPress posts, pages, and custom post types. The pageviews count represents the overall number of views for each content item and is not unique to individual users. By following the steps outlined in this tutorial and applying the provided code snippets, you now have the ability to track and monitor the popularity of your content directly from the WordPress backend.

Remember to always use a child theme to preserve your modifications during theme updates. Feel free to customize the code further or explore additional functionalities based on your specific requirements.

Leave your feedback and help us improve šŸ¶

We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee ā˜•ļø or donating via PayPal šŸ’°.

More free knowledge, because why not?

Your thoughts matter, leave a reply šŸ’¬

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