If you have an WordPress based site that allows users or authors to register and perform certain tasks, you may be interested to display the total users count on your site. This may also potentially attract more users to join your site. This can easily be implemented in quite a few ways. However, in this post I will show you two different method which are fairly simple and easy to understand. So, lets get started.
Using WordPress Function
First, we will take a look at the count_users function which can be used to retrieve the users count in WordPress. Here is the snippet.
<?php $count = count_users(); echo 'Total User: ', $count['total_users']; ?>
This is the simplest method and its easy to understand. Simply copy the snippet and paste it wherever you want to display the users count.
Using SQL Query
We can also retrieve the users count from the WordPress "users" table using simple SQL query statement. Here is the code.
<?php $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->users"); echo 'Total User: '.$count; ?>
This is also very simple. What we are doing here is, assigning a variable ($count) that performs an SQL query on our database table (users) and returns the count from it.
Fact of the matter is, we are doing pretty much the same thing on both cases. The only difference is, on first method we are calling a built-in WordPress function to get the count and on second method we are performing an SQL query. That's all.
I hope you found this tutorial to be useful. Feel free to share it and subscribe my RSS feed. Thank you.