Skip Navigation

[Resolved] Create a View to display total number of results from a query

This support ticket is created 6 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 3 voices.

Last updated by Ben 6 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1091846

Ben

Probably quite a simple one but I've had a search and a fiddle around but I still can't figure out how to achieve what I want.

How would I go about creating a View to show the total amount of results in a query.
For example, if there were 10 posts connected to another using relationships, I would simply want to be able to output "10".

#1091870

Create a View, query the Post Type of which you want to get back results, and set a Query Filter so to get posts related to the one where this View is inserted.
https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/displaying-related-child-posts/
https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/

Then, edit the Loop and insert (before the <wpv-loop> tags, so it does not repeat) the ShortCode to either display total of items or paginated amount of items found.
https://toolset.com/documentation/user-guides/views-shortcodes/#displaying-data
https://toolset.com/documentation/user-guides/views-shortcodes/#vf-153270
https://toolset.com/documentation/user-guides/views-shortcodes/#vf-155378

#1091910

Ben

Thank you for your help Beda, that was perfect!

Thank you for steering me towards those Shortcode reference guides, very useful for the future to stop me bugging you lot!

One more question if I may, would it be possible to do similar, but instead of display the total post count, display a total of custom fields added together from the queried posts?

#1091911

No, that is not possible.

You would instead crate a Custom Field that probably would use get_post_custom_keys(), and then count that array.
However I would not suggest that, it may be expensive in terms of performance:
https://codex.wordpress.org/Function_Reference/get_post_custom_keys

It is however the one way, to create a custom ShortCode that somehow gets all the meta from a post (get_post_meta() works as well, just that one requires the field to be known, sam as our ShortCodes to display them).
If no meta_key is passed to get_post_meta() as well you get all meta of that post, but along with that as well their content.
Even more expensive than get_post_custom_keys():
https://developer.wordpress.org/reference/functions/get_post_meta/

#1092353

Hi, Beda is off today. Please let me know if you need additional assistance here.

#1092370

Ben

Thanks for the offer Christian, I've gone about this another way now.

Thank you for your help to Beda!