webappguides
Create Wordpress Theme From Scratch! Build Your Own

How to count author posts in WordPress

webapps log

webapps Lab
January 6, 2021
ThemeFunctions

In this post, you are going to find a WordPress function that returns the number of posts the author has created.

That function is count_user_posts( ).

To show the number of posts of the author, count_user_posts requires the ID of a user.

The function must receive the integer argument representing the id of the author.

Passing the user-id alone to the function counts all author posts without considering post types.

The default post type the function returns is post.

So, count_user_posts( ) requires the second parameter to count  other post types.

This is the better way on how to count author posts in WordPress.

Shortcode

<?php echo count_user_posts(2);?>

count_user_posts function receives an integer, which is 2.

The integer representing the id of the author.

So, the function returns the number of all posts of the type ‘post’ from the author with id 2.

The number of posts returned involves posts with the status of either public or private.

However, count_user_posts can bring the count of author posts from multiple post types, private only or public.

Counting Author Multiple Post Types Posts

WordPress developer page states count_user_posts can pass the string array containing various post types.

This means it is going to give the number of posts from the multiple post types.

To count author posts of various post types at once, there are two steps to be considered.

First, create an array that lists the post types available in the blog.

Last, set the array as the second parameter of count_user_posts.

Remember, the first parameter is an author id.

To cut off the story, this PHP code summarizes the above description.

<?php

$args=array('parks','mountains','hiking','food');

echo count_user_posts(2, $args);

?>

Counting Author Public Posts only.

Sometimes a blog administrator may want to count the author posts with public status only or private status only.

You should set a third parameter of the function either false or true to get the number of posts.

The third parameter is a boolean variable of $public_only, which is set to false by default.

Declaring $public_only to false makes the function retrieves the number of posts of both private and public.

In order to get the count of only author public posts, the $public_only should change to true.

This is what it means.

<?php

echo count_user_posts(2,true);

?>

That will give a number of public posts the user wrote.

Retrieving names of post types dynamically

When you create a WordPress theme for someone else it is hard to predict all custom post types.

Hence, creating an array of post types names is impractical for this case.

The get_post_types function can return the array containing all registered post types.

Therefore, count_user_posts will receive the array brought by get_post_types to count all posts from the author.

This is how to do it.

<?php

$postTypes = get_post_types();

echo count_user_posts(2, $postTypes);

?>

With this, no need to create an array to list the names of post types to get the number of posts of the author.

Get author ID by get_users()

The user id is a required parameter in a count_users_posts to count the author posts.

The question is,

How will you know users ids if the theme is used by someone else that you don’t know?

A good solution to identify id is to call the get_users function.

get_users returns the array containing the list of users, in this, it is a list of the authors a blog has.

1.<?php

2.$authors = get_users();

3.echo count_user_posts($author[0]);

4.echo count_user_posts($author[1]);

5.echo count_user_posts($author[2]);

6.echo count_user_posts($author[3]);

7.echo count_user_posts($author[4]);

8.?>

The above lines of codes assume a blog has five authors.

Line-2 creates an array of users ID, the variable $authors is set to a get_users function.

Line-3 to Line-7 display the number of posts of each writer.

Learn more about this on the WordPress website

How count_user_posts count author posts?

Up to this point, you have already known how to use count_user_posts to calculate the number of posts of a specific author.

But it is not bad to understand its raw source.

This function uses three functions to know how many posts the author has created.

These functions include.

  1. get_var
  2. get_post_by_author_sql
  3. apply_filter

get_var

This function is called to pass the SQL selection count query to count the row of a specified table.

The SQL query passed in get_var requires a WHERE condition clause statement to avoid counting unrequired rows.

WHERE clause must be returned by get_post_by_author_sql.

get_post_by_author_sql

The get_post_by_author_sql function outputs SQL WHERE statements.

The function takes the three parameters of the count_user_posts.

So, a where conditional clause includes some data of the count_user_parameters.

The returned value is then going to get_var to count all rows that match with the given criteria.

apply_filter

In count_user_posts, apply_filter passes five parameters to filter the author number of posts.

These parameters include the get_usernumposts action hook, get_var, and three parameters from count_user_posts.

Note that get_var counts the number of rows.

And the apply_filter will filter posts basing on the data settings of count_user_posts arguments.

To summarize this, count_user_posts returns apply_filter value, which is the number of posts the author wrote.

The raw source can be found in the wp-includes/users.php file

Wrap-up

The easiest way to count posts written by the author is to use count_user_posts but this is not the only function that serves that purpose.

In fact, get_usernumposts() can also reveal the count of author posts.

But both functions require a user id, however, count_user_posts  is more flexible as it allows to pass extra useful data.

get_usernumposts function passes only one argument meaning that your freedom to count author post types is limited.