Skip Navigation

[Resolved] Query only posts that have relationship items assigned in many to many relation

This support ticket is created 3 years, 7 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
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: Africa/Casablanca (GMT+01:00)

This topic contains 3 replies, has 2 voices.

Last updated by Jamal 3 years, 7 months ago.

Assisted by: Jamal.

Author
Posts
#2008821
Look Books Many to many relationships.png

Tell us what you are trying to do?
I've CPT called Look Book which has many-to-many relationship with 'Products' CPT

Relationship Name is: 'Look Books Products' (slug: look-book-product)

Some of the products have associated post from Look Book CPT.

I have a view that i want to filter and show only those products which have associated look book items.

PFA screenshot.

p.s.: Please note i don't want nested view. I only want to filter products have look book items.

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?

#2009057

Hello and thank you for contacting the Toolset support.

I would suggest setting the view to query the intermediary post type of the relationship, and inside of the loop use the item-attribute or the dynamic sources(using blocks) to display the parent product post.
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/item-attribute/

Note that you might have duplicated products, especially those linked to more than one Look Book post.

Otherwise, you can implement a custom code solution using the Toolset Relationship API, or our extension to the WP_Query arguments:
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#wp_query-argument-for-querying-by-related-posts

I hope this helps. Let me know if you have any questions.

#2010817

Hi Jamal,

I'm still confused and can't comprehend how to implement this.

I've already created a view that queries products.

I just want to find out if there exists a relationship. i..e I only want to pull the products that have associations.

I've a problem which is opposition of the problem over here:
https://toolset.com/forums/topic/views-within-a-view/#post-1784287

In the above code, it excludes posts with specific relationship. I want code which only 'includes' posts with specific relationship.

#2010879

You can use a similar code to that example https://toolset.com/forums/topic/views-within-a-view/#post-1784287
By changing lines 25-27. Instead of including only the post that do not have related posts, include only the ones that have a related post.

      if( ! empty($products) ){
        $include_ids[] = $showroom->ID; // push the related project IDs into this array
      }

Does it make sense?