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 ) ); ?>

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

Leave a Reply

Your email address will not be published.