Skip Navigation

[Resolved] View filter with complicated relationship

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

Problem: I would like to filter posts by properties of another related post

Solution: Use custom code to perform multiple queries and compare the results of those queries.

This support ticket is created 6 years, 10 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 2 replies, has 2 voices.

Last updated by gavinS-2 6 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#614256

I'm trying to create a complicated view which is making my brain explode. I hope you can help.

I have a type 'item-model' which is a parent of two other types 'request-line' and 'item'.

'item' and 'request-line' have a many to many relationship for which I have created another type 'request-line-item' which is a child of both.

'request-line' is also a child of another type 'request'.

I am trying to create a nested view on the single post page for a specific 'request'. The top layer of the view would be request-lines which are children of the current request post.

The next level is the complicated part.

items have a 'price' field and a serial number which I am storing as 'post-title'.

request-lines have a 'start-date' and an 'end-date' fields.

I am trying to query items to find the items of a specific item-model which have not been booked out between the 'start-date' and 'end-date' of the current request-line. (Available items of a certain item-model between these dates)

Ie. The current request-line is a booking for a specific item-model between two dates (start-date and end-date). I am trying to display the serial number (post-title) and the 'price' for all items of that item model that have no existing request-lines already booking it between those two dates. So then it would be available to be booked on this request-line (and so a request-line-item) would be created to make the booking for the specific item, rather than the item-model which was requested.

Ie someone wants to book a camera of a specific make and model between specified dates. I am trying to check which individual cameras (items) with their own unique serial number of that make and model (item-model) are available between those two dates.

Do I make sense?

#614476

Hi, you are making sense but unfortunately Views isn't smart enough to filter one custom post type by the custom field values of a different post type, much less intersecting multiple levels of relationship queries. Your situation calls for some very custom code that performs multiple WP_Query requests and compares the results of those queries to produce a result set. We do not provide that level of custom code here in the forums. Here's what must be done:
- Find all child request-line-items of the current request-line. Child posts contain a reference to each of their parent posts in the postmeta table under the key format "_wpcf_belongs_parentslug_id", so a meta query will be required.
- Use get_post_meta to build an array of IDs of the item parents of all those request-line-items. Keep this set of IDs in an "exclusion" array - these represent items in the same parent item-model that are no longer available for the current time period.
- Use get_post_meta to find the ID of the parent item-model of the current request line.
- Build an array of IDs of all item children of that item-model. This represents all the items in the parent item model, even those that are no longer available.
- Subtract the exclude array from this full item array, and now you have a set of items that are still available- i.e. the items of the parent item-model shared by the current request-line, that are not currently associated with the current request-line item via another request-line-item.

If you are capable of writing your own code, I can help you troubleshoot anything related to the Toolset APIs or data models. If you need assistance with other portions of the code, you may be able to connect with a professional developer in our Contractors portal: wp-types.com/contractors.

#614542

Wow thanks Christian

This is really helpful, I'm going to try and see if I can figure out the code

You're a hero