Skip Navigation

[Resolved] Find ‘child’ posts that are NOT in a M2M relationship with a ‘parent’ post

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

Problem:

Within a many-to-many relationship between to Custom Post Types, for a given 'parent' post, I need to see a list of 'child' posts which are NOT already part of a relationship with that particular 'parent' post.

Solution:

You can setup custom codes with filter hook "wpv_filter_query" to apply custom filters to your view, for example:

https://toolset.com/forums/topic/find-child-posts-that-are-not-in-a-m2m-relationship-with-a-parent-post/#post-1175075

Relevant Documentation:

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

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

Last updated by John Johansen 6 years ago.

Assisted by: Luo Yang.

Author
Posts
#1174862

Tell us what you are trying to do?
Within a many-to-many relationship between to Custom Post Types, for a given 'parent' post, I need to see a list of 'child' posts which are NOT already part of a relationship with that particular 'parent' post.

In short, I suspect that I want an outer-join of the child posts relative to the relationships.

Is there any documentation that you are following?
Toolset post relationships.
Is there a similar example that we can see?
No, but I can describe my situation.
I have two custom post types, Patrons and Locations.
I have created a many-to-many relationship between the two custom post types. Patron is the 'parent', Location is the 'child'.
A Patron will select from a list of Locations in order to establish each relationship.
However, as the Patron selects from a list of Locations, I only want the Patron to see Locations which he has NOT already selected (rather than seeing the entire list of Locations).
What is the link to your site?
hidden link

#1175075

Hello,

There isn't such a built-in feature within Views plugin, as a workaround, you can setup custom codes with filter hook "wpv_filter_query" to apply custom filters to your view, for example:

1) Create a normal post view to query all "Locations" posts

2) Use wpv_filter_query filter hook to trigger a custom PHP function
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

3) In this PHP function, get all related "Locations" posts with function toolset_get_related_posts(), you will be able to get the those "Locations" post IDs, pass IDs to View's query, and exclude them:
https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
post__not_in (array) - use post ids. Specify post NOT to retrieve. If this is used in the same query as post__in, it will be ignored

#1175133

My issue is resolved now. Thank you!