Skip Navigation

[Resolved] show number of users that hav published a post via cred

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the customer wanted to count the number of users who have created posts under a specific post type.

Solution:

This is actually something that will require a bit of custom coding to achieve.

Here below is a small snippet that was written to help resolve this.

function count_users_with_posts() {
 
     $blogusers = get_users();
     $user_count = array();
  foreach($blogusers as $user){
    $args = array(
    'post_type'  => 'covid-19-info',
    'author'     => $user->ID,
);
$wp_posts = get_posts($args);
    if(count($wp_posts) > 0){
     array_push($user_count, $user->ID); 
    }
  }
 
     return count($user_count);
 
}
add_shortcode( 'count_users_with_posts', 'count_users_with_posts' );

Add the above to your Toolset custom code section in Toolset->Settings->Custom Code and activate it. You will need to replace "covid-19-info" with the slug of your post type that you want to count the users for.

This support ticket is created 4 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 3.96 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by hannesU-2 4 years, 2 months ago.

Assisted by: Shane.

Author
Posts
#1768409

Hi there. I am using views to display infos that are uploaded via cred form to my page. On the landing page i want to display some statistics.
And there is one part where i am stuck since this morning:
I want to display the number of active members that posted at least one post / info.
So I am using a view to search for all members. And then i wanted to use a second view inside that first one to filter out the number of members that have published something... I can´t make it....

Can you help?

#1768501

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Hannes,

Thank you for getting in touch.

Essentially you want to only display the users that have created posts correct? So if they haven't made any posts you don't want to display them in the list that your view is displaying ?

Please let me know if this assumption is correct.

Thanks,
Shane

#1768539

Hi Shane,
actually I just want to display the number of users that have at least posted one post type.
So i have created a member group for users.
They register and can post via cred form.

On one Page i am listing some statistics. And want to show the number of users who have published content.

#1768553

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Hannes,

In order to do this it would be best to use a custom shortcode to do the count.

This can be done using the shortcode below.

function count_users_with_posts() {

	 $blogusers = get_users( 'orderby=post_count&who=authors' );
	 
	 return count($blogusers);

}
add_shortcode( 'count_users_with_posts', 'count_users_with_posts' );

Add the following shortcode to the Toolset custom code settings in Toolset->Settings -> Custom Code and ensure that you've activated the snippet.

To use the shortcode just use [count_users_with_posts]

Please let me know if this helps.
Thanks,
Shane

#1768855

Hi Shane,
unfortunately this is not working correctly.
When using your shortcode I receive a number of one.
That is the admin, that has posted one post (Hello World).
The Users I want to count have the user role EC Member and the posts i want to count are not "standard blog posts" but custom post types (slug = covid-19-info).

#1769489

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Hannes,

I've revised the code a bit more as I realised that it was not providing the accurate information. I've also allowed it to specify the post type that you want to add the count for.

function count_users_with_posts() {

	 $blogusers = get_users();
	 $user_count = array();
  foreach($blogusers as $user){
    $args = array(
    'post_type'  => 'covid-19-info',
    'author'     => $user->ID,
);
$wp_posts = get_posts($args);
    if(count($wp_posts) > 0){
     array_push($user_count, $user->ID); 
    }
  }

	 return count($user_count);

}
add_shortcode( 'count_users_with_posts', 'count_users_with_posts' );

This should now work. Please update your code with the one above and let me know if the issue still remains.
Thanks,
Shane

#1770079

Thank you, Shane! That made it work.
Great Support as always!

Best, Hannes