Skip Navigation

[Resolved] Counting Custom Fields

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

Problem:

Using Toolset I created a parent "Sector" which contains a number of "Routes"

Each "Route" has a custom field "Difficulty Grade", which I can assign via a Select Field

I would like to create a View to display the number of routes for each "Difficulty Grade"

i.e.

- 2 Routes graded 4
- 10 Routes graded 5
- 15 Routes graded 6
- 7 Routes graded 7

Solution:

You can create a post view, query "Routes" posts, filter on custom field "Difficulty Grade" by shortcode attribute, for example "difficultygrade"

And display only the shortcode [wpv-items-count]

Then display the same view multiple times, like this:

[wpv-view name="users" difficultygrade="3"]
[wpv-view name="users" difficultygrade="4"]

Relevant Documentation:

https://toolset.com/documentation/user-guides/filtering-views-by-custom-fields/#source-for-compare-value

https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-found-count

This support ticket is created 6 years 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: Asia/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by ivanT-7 6 years ago.

Assisted by: Luo Yang.

Author
Posts
#1141368
routes 6.jpg

Hello,

Using Toolset I created a parent "Sector" which contains a number of "Routes"
Each "Route" has a custom field "Difficulty Grade", which I can assign via a Select Field

I would like to create a View to display the number of routes for each "Difficulty Grade"

i.e.
- 2 Routes graded 4
- 10 Routes graded 5
- 15 Routes graded 6
- 7 Routes graded 7

I have attached a screenshot of the view I created for this. Could you help me understanding what to correct?

Ivan

#1141980

Hello,

I assume we are talking about this case:
- post type "Routes",
- custom number field "Difficulty Grade"

For example:
Routes A, Difficulty Grade: 3
Routes B, Difficulty Grade: 4
Routes C, Difficulty Grade: 4

You are going to display post counts for each "Difficulty Grade", like this:
Difficulty Grade "3“: 1 "Routes" post found
Difficulty Grade "4“: 2 "Routes" post found

If it is, it is possible within Views plugin, you can create a post view, query "Routes" posts, filter on custom field "Difficulty Grade" by shortcode attribute, for example "difficultygrade"

https://toolset.com/documentation/user-guides/filtering-views-by-custom-fields/#source-for-compare-value

And display only the shortcode [wpv-items-count]

https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-found-count

Then display the same view multiple times, like this:
[wpv-view name="users" difficultygrade="3"]
[wpv-view name="users" difficultygrade="4"]
https://toolset.com/documentation/user-guides/passing-arguments-to-views/#accessing-the-arguments

#1142970

Hi Luo,
thank you so much for your assistance.
I have followed your instructions but found a problem.

If I create a view to display the shortcode [wpv-items-count], it works fine as long as the custom field type is a string on a *single line*. Unfortunately the custom field "Difficulty Grade" is a *Select* type.

Is there a way around this?

Also, when using the shortcode [wpv-view name="users" nameexample="John"]
Instead of searching a field *equal to* would it be possible to search *contains* ???

Thanks so much
Ivan

#1143551
Custom-field-content.JPG

Dear Ivan,

Q1) the custom field "Difficulty Grade" is a *Select* type, Is there a way around this?
Yes, you just need to setup the attribute "difficultygrade" value as the option's "Custom field content" value, you can get the "Custom field content" value by edit the custom select field "Difficulty Grade", see screesnhot Custom-field-content.JPG

Q2) Instead of searching a field *equal to* would it be possible to search *contains* ?
You can edit the view, setup the custom field filter "Comparison function" as "LIKE", see the document I mentioned above:
https://toolset.com/documentation/user-guides/filtering-views-by-custom-fields/#comparison-function
LIKE Single value Checks if the custom field value is similar to the compare value, using MySQL LIKE operator

#1145121

My issue is resolved now. Thank you!