When developing WordPress themes (such as user profiles), you often need to calculate and display the total page views for all posts by a specific author. This guide provides an efficient, standard solution with detailed code explanations and usage instructions.
Core Function
The following function sums the page views for a specified author by querying the database directly. It assumes view count data is stored in a post meta field named views, which is common with popular statistics plugins like WP-PostViews.
if ( ! function_exists( 'cx_posts_views' ) ) {
/**
* Get total views for all posts by a specific author.
*
* @param int $author_id Author ID.
* @param bool $display Whether to echo formatted output. If false, returns integer.
* @return int|void Returns view count integer if $display is false.
*/
function cx_posts_views( $author_id = 1, $display = true ) {
global $wpdb;
$sql = $wpdb->prepare(
"SELECT SUM(meta_value+0) FROM {$wpdb->posts}
LEFT JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)
WHERE meta_key = 'views' AND post_author = %d",
$author_id
);
$total_views = intval( $wpdb->get_var( $sql ) );
if ( $display ) {
echo number_format_i18n( $total_views );
} else {
return $total_views;
}
}
}
Code Explanation & Notes
- Security: Uses
$wpdb->prepare()for safe parameterized queries, preventing SQL injection. - Function Existence Check:
if ( ! function_exists( ... ) )prevents duplicate function definitions. - Meta Field Name: The query looks for
meta_key = 'views'. Adjust this if your plugin uses a different key. - Performance: This function performs a JOIN and aggregation. For sites with many posts or high traffic, consider caching the result (e.g., using Transients API) to reduce database load.
Usage Examples
Add the function to your theme's functions.php file, then call it anywhere in your templates.
Example 1: Display Author's Total Views on a Post
// Method 1: Direct call
cx_posts_views( get_the_author_meta( 'ID' ) );
// Method 2: Step-by-step
$author_id = get_the_author_meta( 'ID' );
cx_posts_views( $author_id );
Both methods output the formatted total (e.g., "12,345").
Example 2: Get Raw Number for Custom Processing
$author_id = get_the_author_meta( 'ID' );
$author_total_views = cx_posts_views( $author_id, false );
echo 'Total Reads: ' . $author_total_views . ' times';
// Or use conditionally
if ( $author_total_views > 10000 ) {
echo 'Popular Author!';
}
Example 3: Get Views for Any Specific Author
// Output views for author ID 5
cx_posts_views( 5 );
// Get the number for author ID 5
$specific_author_views = cx_posts_views( 5, false );
echo 'Author ID 5 total views: ' . $specific_author_views;
Extensions & Optimizations
- Caching: Cache the result (e.g., for 12 hours) using Transients API to improve performance.
- Compatibility: Adjust the SQL if your site uses a different method to store view counts (e.g., a custom table).
- Frontend Display: Use the returned value in author profiles, user dashboards, leaderboards, or anywhere author influence is shown.
With this function, you can easily implement author total view statistics in your WordPress site.