Skip Navigation

[Resolved] Querying post relationship by relationship fields

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

Problem: I have two custom post types in a many-to-many relationship. I would like to create a View that is filtered by custom field of a post relationship so I can display specific related posts.

Solution: You must make the intermediary post type visible in wp-admin. If it is not visible, go to Toolset > Post Types and edit the intermediary post type. In the "Options" panel, make sure "show_ui" is checked.

Now create a View and select the intermediary post type in "Content selection". Add the custom field filter in Query Filters. If you cannot see the Query Filters panel, scroll to the top right corner and click "Screen Options" to activate the Query Filter panel. After you add the custom field filter, add a post relationship filter using your M2M relationship. Set it such that the related Place is set by the current post. In the View's Loop wizard you can use the "edit" button to show information like the title of a related post (edit-field.png, post-selection.png). This will allow you to display information from the Business related to the current intermediary post. Finally, place the View in your template for Places.

This support ticket is created 5 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 orlaF 5 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1209420

Tell us what you are trying to do?

I have a two custom post types:
1. Businesses
2. Places

I have created a relationship where businesses can be added to a place as either an 'owner' or a 'stakeholder' using relationship fields.

I want to display two separate lists on each place - one with the owners and one with the stakeholders but I cannot select the relationship field when I am setting the Query Filters - both lists display all Businesses associated with the Place.

This seems like something that should be straight forward - is it possible?

#1209637
edit-field.png
post-selection.png

Hi, it sounds like you have a many-to-many relationship between Businesses and Places, and you have implemented a custom field on the relationship. This field determines whether the relationship is that of an "owner" or a "stakeholder". If I've got that correct so far, then you must make the intermediary post type visible in wp-admin. If it is not visible, go to Toolset > Post Types and edit the intermediary post type. In the "Options" panel, make sure "show_ui" is checked.

Now create a View and select the intermediary post type in "Content selection". Add the custom field filter in Query Filters. If you cannot see the Query Filters panel, scroll to the top right corner and click "Screen Options" to activate the Query Filter panel. After you add the custom field filter, add a post relationship filter using your M2M relationship. Set it such that the related Place is set by the current post. In the View's Loop wizard you can use the "edit" button to show information like the title of a related post (edit-field.png, post-selection.png). This will allow you to display information from the Business related to the current intermediary post. Finally, place the View in your template for Places.

#1209652

Those instructions were excellent and it is working perfectly. Thank you!