Skip Navigation

[Resolved] Is there way to count how many items are in a Post Type or taxonomies

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

Problem:
The user would like a way to count all the posts and terms.

Solution:
This can be done using legacy views, or custom code. Check a detailed explained about the views' solution here https://toolset.com/forums/topic/is-there-way-to-count-how-many-items-are-in-a-post-type-or-taxonomies/#post-2234323

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-found-count

This support ticket is created 3 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.

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

Supporter timezone: Africa/Casablanca (GMT+01:00)

Tagged: 

This topic contains 6 replies, has 3 voices.

Last updated by julB 3 years, 2 months ago.

Assisted by: Jamal.

Author
Posts
#2234185

Tell us what you are trying to do?
I want a way to count how many items are within a post type or taxonomies, and display this on the front end for users to see.

Is there any documentation that you are following?
No

Is there a similar example that we can see?
Similar to this - https://wordpress.org/plugins/counter-number-showcase/

Thanks for any help.

#2234323

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You would need to use the legacy editor for Views to achieve this.

To count how many posts there are published of some type you would create a View that queries that post type, and then in the output section simply uses the wpv-found-count shortcode to output the number of results.

You can do the same to count the number of terms belonging to a taxonomy by creating a View that queries the taxonomy (instead of posts), and—again—outputs the number of results using the same wpv-found-count shortcode.

See https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-found-count

If you are unfamiliar with the legacy editor, you will need to enable it at Toolset > Settings (you may want to expose both the block and legacy editors), then you can create a View at Toolset > Views. (Choose to create a View that shows all results.)

Set up the Content Selection as required, then go down to the loop output section. Insert the wpv-found-shortcode after the wpv-items-found shortcode, like so:

[wpv-layout-start]
	[wpv-items-found]
	[wpv-found-count]
	<!-- wpv-loop-start -->
		<wpv-loop>
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

For a simple View like this you can also check the option "Disable the wrapping DIV around the View" which will simplify the generated markup.

Then insert the View where you want to display the result. (You can insert it using a View block.)

#2235013

My issue is resolved now. Thank you Nigel 🙂

#2235289
nyH2Sf.png

Hi, thanks for the detailed reply, I have got the count working on the post types, but when I try to create a view for taxonomy and view it using the view block, it gives the following error
" Error while retrieving the View preview. The selected View (ID: undefined) was not found."

It also shows as blank when I try to select the taxonomy view I just created. (see screenshot) there should be 3 taxonomy views there.

I am using a taxonomy called factions, this then has 3 separate sub-factions within. So I would need to add a filter (in Edit View) to select 1 of these factions?

Thanks again for the help.

#2235303

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screenshot 2021-11-26 at 14.58.33.png

Taxonomy Views are not supported in the block editor, only post Views.

You can insert the View using a wpv-view shortcode, which can be entered into a block.

The easiest way to generate the required shortcode is to add a Fields and Text block, switch to HTML mode, then use the little Toolset icon to open what is known as the Fields and Views button, and from the resulting dialog you should be able to choose your taxonomy View(s) from the bottom.

You can edit the shortcode to add any attributes as required.

#2235315

Besides the view's based solution that Nigel suggested, you can also build your own custom shortcodes to return the count of posts or terms. You can use the following WordPress functions:
- https://developer.wordpress.org/reference/functions/wp_count_posts/
- https://developer.wordpress.org/reference/functions/get_posts/
- https://developer.wordpress.org/reference/functions/wp_count_terms/
- https://developer.wordpress.org/reference/functions/get_terms/

Check our article on how to create custom shortcodes here https://toolset.com/documentation/programmer-reference/adding-custom-code/how-to-create-a-custom-shortcode/

#2235325

Thanks for the great support. My issue is resolved now. Thank you!