Notifications
Clear all

How to add author posts' view count in author page

0 Posts
1 Users
0 Reactions
4,053 Views
(@varos)
Estimable Member
Joined: 8 years ago
Posts: 141
Topic starter  

Dear users,

Please use this code and make sure that your WordPress is 4.1 or higher version:

add_filter('get_the_archive_title', 'apsw_the_archive_title');

if (!function_exists('apsw_the_author')) {
    function apsw_the_archive_title($title) {
        global $wpdb, $post;
        if ($wpdb && $post && $post->post_author && is_author()) {
            $sql = "SELECT SUM(`stats`.`view_count`) FROM `" . $wpdb->prefix . "sw_statistics` AS `stats` INNER JOIN `" . $wpdb->prefix . "posts` AS `p` ON `stats`.`post_id` = `p`.`ID` WHERE `p`.`post_author` = %d;";
            $sql = $wpdb->prepare($sql, $post->post_author);
            $userPostsViewsCount = $wpdb->get_var($sql);
            if ($userPostsViewsCount) {
                $title .= " posts views($userPostsViewsCount)";
            }
        }
        return $title;

    }
}

   
Quote
Share:
Scroll to top