how to show user post count in wordpress

Many people asked me, how to show specific users post count in wordpress widget area. Here in this article, We will give you code and trick for showing user post count in widget area.

There is inbuilt wordpress method called “count_user_posts” which will help you to show users post or page or custom post count in your wordpress theme. Since wordpress 3.0 version this method exits in API. You can use “count_user_posts” method for many purpose. You can pass two parameters to this function, If you did not pass second parameter than it will be post.

Here we given some samples:

echo count_user_posts(1,'post');             // posts
echo count_user_posts(2,'page');             // pages
echo count_user_posts(3,'products'); // custom post types

show user post count in wordpress

We used above method for showing post count as follows:

show user post count in wordpress
show user post count in wordpress

I used PHP Code Widget plugin for adding PHP code in widget section.

More about PHP Code Widget

The normal Text widget allows you to insert arbitrary Text and/or HTML code. This allows that too, but also parses any PHP code in the text widget and executes it.

This can make it easier to migrate to a widget-based theme. However, this plugin should not be used long term, as anybody with access to edit the widgets on your site will be able to execute arbitrary PHP code.

All PHP code must be enclosed in the standard php opening and closing tags ( <?php and ?> ) for it to be recognized and executed.

Only users with the unfiltered_html role will be allowed to insert unfiltered HTML. This includes PHP code, so users without admin or editor permissions will not be able to use this to execute code, even if they have widget editing permissions.

How used following code in PHP widget.


<?php printf( __( 'Number of posts published by user: %d', 'text-dom-here' ), count_user_posts( 1 ) ); ?>

Count post views without wordpress plugin using meta data

Showing post views is nice feature. Many client want to show post/page views on pages. Where in this wordpress tutorial, we will show, How to show post views using post meta data.

There are many wordpress plugins available for counting post views. Here are some, If you don’t want to modify your wordpress theme code.

Count post views without wordpress plugin using meta data
Count post views without wordpress plugin using meta data

Automated method

Add following wordpress plugin and show post views

WP-PostViews

Enables you to display how many times a post/page had been viewed.

Post Views Counter

Post Views Counter allows you to display how many times a post, page or custom post type had been viewed with this simple, fast and easy to use plugin.

For more information, check out plugin page at dFactory or plugin support forum.

Features include:

  • Option to select post types for which post views will be counted and displayed.
  • 2 methods of collecting post views data: PHP and Javascript, for greater flexibility
  • Option to set time between counts
  • Excluding counts from visitors: bots, logged in users, selected user roles
  • Excluding users by IPs
  • Restricting display by user roles
  • One-click data import from WP-PostViews
  • Post views display position, automatic or manual via shortcode
  • W3 Cache/WP SuperCache compatible
  • WPML and Polylang compatible
  • .pot file for translations included

Manual Method

function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' Views';
}
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); 

Add above code into functions.php file which you can find in theme folder.

Step 1
After this you need put following in single.php file and page.php file. Inside post loop, copy and paste following code.

<?php
          setPostViews(get_the_ID());
?>

Note: If you put above code in wrong place than it will not work.