Skip Navigation

[Resolved] Dispalyount of related custom post

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 1 reply, has 2 voices.

Last updated by Luo Yang 1 year, 5 months ago.

Assisted by: Luo Yang.

Author
Posts
#2488509

Bob

I'm sure this is straightforward but I'm tying myself up in knots here.
It's a library lending system I have two custom post types Items and Loans - when someone borrows a Book a record is created in Loans.
What I need to do is show a count of all Loans for a certain type of Book (the type is a field in the Book type) ideally within a date range (the date is held in Loans)

I think I'm confusing relationships with filters so any guidance would be appreciated.

#2489155

Hello,

The Book Type field is in Book post type, you can not filter the "Loan" posts by fields of another post type "Book ".

So it needs custom codes, for example:
1) Create a post view:
- Query Loans posts
- Filter by the date field
- Display the Loans post information + post found count:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-found-count

2) Use wpv_filter_query filter hook to trigger a custom PHP function, in this PHP function:
a) Get Book post IDs by book type parameter:
https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters
b) Use above Book post IDs to get all related Loan post IDs:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
c) Pass above Loan post IDs to View's query as parameter "post__in":
https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters

More help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

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