Skip Navigation

[Resolved] Filter a View of child posts by parent post taxonomy terms

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

Problem: I would like to create a View that filters child posts (CPT A) by the taxonomy terms applied to their parent posts (CPT B).

Solution: It is not possible to filter a View of CPT A by taxonomy terms applied to CPT B using the standard View editor. You can use the Post Relationship API to modify the View's query using PHP.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

This support ticket is created 5 years, 11 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 1 reply, has 2 voices.

Last updated by Christian Cox 5 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#819081

I don't know if I understand the use of custom types the right way... I have a db manager background and I was expecting Types (with Views) to work the same way, especially with the beta versions with relationships...

Here's my situation:
I have a condo with two different phases (i.e. buildings). The first phase is for location and the second one will be for sale... Each condo has many levels and each level has many rooms or apartments... We also have types of room for specifics like dimensions, number of bedrooms, etc.

So, I created a type called Phase, that at first only was a parent of levels... I added to it categories (taxonomy) to say whether it's for location or sale... One phase will have many levels, so I created the type "levels" (étages in French), that only has it's name a WYSIWYG editor to put a shortcode of an image map plugin of the floor... Since strangely I can't access the name (which would be the title) in Views (don't know why), I added another "Name" field to use it in a search filter... Phase is the parent of "Levels" and "Levels" is the parent of "Units".

Then I have "Units" that has it's title (number of the apartment), a type, which is in fact a link to the "Type of room" type and a status, to say whether it's free, rented or sold. Finally, I have "Type of room" type, which is also a parent of "Units", because one type can have more that one unit (i.e. there is about 25 different types, but probably more than a hundred apartments).

So, I tried to create a view where I can use the level as the filter, to show the name, floor plan and an image of the selected level. The floor plan is an image with image maps pointing to units.

So, I tried to select "Phase" and "Levels" for my View... Like I previously said, I wanted to use the name (i.e. title) as the filter in the drop-down, but I can't, so I created a field name to use it... The category is filtered by the shortcode calling the view (which is tied to the "Phase"), but it doesn't work at all... No matter whether I choose location or sale as type, they all show up (or neither with different settings)...

What I would like to do is use the filter of the "Phase" to select only the levels of the "locations" and then use that in the filter and all... I know I could do it with multiple nested Views, but I'm afraid it will increase the size and the execution time of it all... Since we can select more than one type (checkboxes and not radio buttons), there is surely a way to use relationships (parent-child for now and relationships in next version) to filter it... But, from what I understand, it probably applies the "location" category to the "Levels" instead of "Phases" and therefore don't apply it at all...

I've learned that to show something (like the post title), we need a code like this one: [types field="xxxx" id="$yyyy"][/types]... so don't know if we need to do something similar in the filter...

Guess it will be easier if I give you access to my staging server...

#834885

But, from what I understand, it probably applies the "location" category to the "Levels" instead of "Phases" and therefore don't apply it at all...
Yes, that is correct. A View of post type A cannot be filtered by the taxonomy terms applied to its parent post in post type B. You will need to write custom code to handle this, or use nested Views. We have some documentation available for the Post Relationship API here:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
Query the parent post type with the correct taxonomy term filter to determine the correct Phase post ID, then use the API to modify the Level View's query and insert the correct parent post ID in the toolset_relationships array.

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