Blog / WordPress/ How to Get and Display Total Post Views in WordPress

How to Get and Display Total Post Views in WordPress

WordPress 如何获取并显示站点文章总访问量(浏览量)

Many WordPress themes display the total site view count in the footer or sidebar, which helps showcase a blog's popularity. This article explains how to create a custom function to retrieve the total views of all WordPress posts and how to display it on the front end.

Core Function: Get Total Views

Add the following PHP code to the end of your current theme's functions.php file. This function queries the database for the 'views' custom field (assuming your view count is stored there) and sums the values to get the total.

/**
 * Get total views for all WordPress posts.
 *
 * Queries the `postmeta` table for records where `meta_key` is 'views'
 * and sums the `meta_value` values.
 *
 * @return int Total site views as an integer.
 */
function get_total_site_views() {
    global $wpdb;
    $total_count = 0;

    $view_records = $wpdb->get_results("SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = 'views'");

    foreach ( $view_records as $record ) {
        $view_count = $record->meta_value;
        if ( is_numeric( $view_count ) ) {
            $total_count += (int) $view_count;
        }
    }
    return $total_count;
}

Notes & Considerations

  • Function Name: The example uses get_total_site_views(). You can rename it, but ensure consistency when calling it.
  • Data Field: This code assumes each post's view count is stored in a custom field named views. This is the default for many popular plugins (like WP-PostViews) and themes. Verify your actual field name.
  • Performance: If your site has many posts, frequent calls may strain the database. Consider using a caching mechanism like the WordPress Transients API.

Displaying the Total on the Front End

In your theme template files (e.g., footer.php, sidebar.php), use the following code to call and output the total.

<?php
echo number_format( get_total_site_views() );
?>

Note: The number_format() function formats the output (e.g., 123456 becomes 123,456) for better readability.

Advanced Optimization: Add Caching

To avoid database queries on every page load, use the WordPress Transients API to cache the result. Here's an optimized version:

/**
 * Get cached total site views.
 *
 * Data is cached for 12 hours by default to improve performance.
 *
 * @return int Total site views as an integer.
 */
function get_cached_total_site_views() {
    $cache_key = 'total_site_views_cache';
    $expiration = 12 * HOUR_IN_SECONDS;

    $total_views = get_transient( $cache_key );

    if ( false === $total_views ) {
        global $wpdb;
        $total_views = 0;
        $view_records = $wpdb->get_results("SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = 'views'");

        foreach ( $view_records as $record ) {
            $view_count = $record->meta_value;
            if ( is_numeric( $view_count ) ) {
                $total_views += (int) $view_count;
            }
        }
        set_transient( $cache_key, $total_views, $expiration );
    }
    return $total_views;
}

After switching to this optimized function, update your front-end call to use get_cached_total_site_views(). The first visit will run the query; subsequent visits within 12 hours will read from cache, improving page load speed.

Following these steps, you can display an accurately counted and performance-optimized total view count anywhere on your WordPress site.