Skip Navigation

[Resolved] Query record with count

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

Problem:
How to return a count of posts for each user in loop (Views)

Solution (include the steps to follow here):

function user_posts_count($atts) 
{
 return count_user_posts($atts["user-id"], $atts["post-type"]);
}
add_shortcode( 'user-post-count', 'user_posts_count' );

Register the ShortCode in Views > Settings > Compatibility > 3rd party ShortCodes and then use it in your View listing Users as this:

[user-post-count user-id="[wpv-user field='ID']" post-type="post"]

Above will return the count of Posts a certain (in Loop) user has created.
Change "post" to your post type slug.

This support ticket is created 8 years, 5 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.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by kelvinL-2 8 years, 5 months ago.

Assisted by: Beda.

Author
Posts
#347465

Dear Beda,

I have a CP booking and I want to have a View to show the count summary of the booking belong to each author?

How can I set the query in View?

Best regards,

Kelvin.

#347590

Thank you for contacting us here in the Support Forum

1. I have a CP booking and I want to have a View to show the count summary of the booking belong to each author?

I understand you want the "Post count" for each author, where you count the Amount of Posts (Bookings) each WordPress user authored?

You can do this with the ShortCode wav-items-count, which gets the number of posts, users or taxonomies that will be displayed on the page.
https://toolset.com/documentation/views-shortcodes/#wpv-items-count

If paginated, use wav-found-count which displays the total number of posts, users or taxonomies that have been found by the Views query. This value is calculated before pagination, so even if you are using pagination, it will return the total number of posts matching the query.
https://toolset.com/documentation/views-shortcodes/#wpv-found-count

Since you want to count Posts, use this on a Post View, querying your Custom post type and set a query Filter to return only the Author's Posts you want to count-

Depends how you set / insert this View, but I recommend to pass the author with a ShortCode argument, so you can use the same View over and over again.
https://toolset.com/documentation/user-guides/passing-arguments-to-views/

Please let me know if you need further infos about this.

Thank you for your patience.

#347850

Dear Beda,

It seem both the [wpv-items-count] and [wpv-found-count] only show the count according to current page belonging to the author. This is not what I want.

For example, there are 100 booking belong to 10 authors in the site, I want to have a page to show the count summary for all authors at the same page.

Not sure whether any shortcode can simply passing the author id and then count the number of the bookings.

Best regards,

Kelvin.

#347965

Thanks for the Details

If you want to use Custom Code, you can build a User View, and use this ShortCode in your functions.php:
https://codex.wordpress.org/Function_Reference/count_user_posts
https://codex.wordpress.org/Shortcode_API

Example:
functions.php:

function user_posts_count($atts) 
{
 return count_user_posts($atts["user-id"], $atts["post-type"]);
}
add_shortcode( 'user-post-count', 'user_posts_count' );


Register the ShortCode in Views > Settings > Compatibility > 3rd party ShortCodes and then use it in your View listing Users as this:

[user-post-count user-id="[wpv-user field='ID']" post-type="post"]

Above will return the count of Posts a certain (in Loop) user has created.
Change "post" to your post type slug.

Please let me know if you need further infos about this.

Thank you for your patience.

#348336

Thanks for your advice.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.