
In this article we will learn how to display total number of posts that a user has created. Most of website also shown their contributors with their contributed posts that shows a beautiful impact on visitors.
By inserting two types of code, we can get our objective easity. I have found the following codes from internet that work fine. Just copy the following code and insert it into function.php page۔ if you dont know how to access function.php, just follow these steps,
- Log in to the WordPress Admin interface
- In the left sidebar, hover over Appearances, then click Theme Editor
- In the right sidebar, click functions.php
This will bring up the functions.php code editor. You can write code directly in this interface and save it. Copy the following code and insert it in function.php page.
* Return the total number of user post
*
* @params $userid INT
* @params $post_type STRING
* @return $user_post_count INT
*/
function rs_count_user_posts( $userid, $post_type = ‘post’ ) {
if( empty( $userid ) )
return false;// if we get there, great so all fine and ready to go
$args = array(
‘post_type’ => $post_type,
‘author’ => $userid
);$the_query = new WP_Query( $args );
$user_post_count = $the_query->found_posts;return $user_post_count;
}
Now after inserting above code, we have now created function.
How to use?
Now the second step is to insert the another code anywhere in the page where you want to show total number of posts of a specifir user.
// display the total number of posts of user with ID of 2 in ‘jobs’ post type
echo rs_count_user_posts( 2, ‘jobs’ );
Done. Just save and refresh the page to show the script.
