Skip Navigation

[Resolved] Filtering many-to-many relationships

This support ticket is created 6 years, 5 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 5 replies, has 2 voices.

Last updated by Nigel 6 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#590409

Tell us what you are trying to do?

I have a many-to-many relationship established and it works fine. I have Products and Artists and a child related custom post called Products-Artists.

I have a view that is filtering with: "Select posts that are children of the Post where this View is shown."
My content template uses:

[wpv-post-title id="$product"]

I need to filter the results on products that are connected to Artists by custom fields and other data set on the Product custom type. Attempts to use the filter do not work, since this view is selecting the content, Products-Artists.

Is there any documentation that you are following?

I have looked through the documentation and have yet to find a solution. This is probably because I am not articulating the problem correctly.

Is there a similar example that we can see?

What is the link to your site?

hidden link, although the view's results are hidden by css. Look for the class available-artwork-for-sale, to expose the section that is rendered by my view.

#590506

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Darcy

Views are built on top of the WordPress class WP_Query used to retrieve posts from the database. It will return posts subject to the posts' own properties (standard WP fields such as post_title as well as custom fields stored in wp_postmeta).

But from your description it sounds like you want to return posts based not on properties of the posts themselves but on properties of something else.

So, for example, with a playground of children you might tell all the children wearing hats to stand on the left, simple enough.

Your request sounds more like telling all the children whose parents have a combined income over €100k to stand on the left. But you don't know what the income of the parents of the children is, and guessing from the labels on their coats doesn't count.

This would work if the children came to school with a badge showing their parents' income.

In terms of querying the database that would be akin to custom table joins with SQL, and goes beyond the querying possible with WP_Query (and, therefore, Views).

If you describe what you are trying to do with this query and how the data is set up, I may be able to help you find a solution, but it may be something that requires writing custom DB queries.

As you probably know we are currently working on a major overhaul of post relationships which will open up the possibility to add such an ability. I contacted the lead developer who has accepted this as a feature request, so that it should be added at some point, though I can't say when.

#591051

That is a hilarious analogy!

I look forward to overhaul of post relationships.

I feel like my issue cannot be unique or even rare. How does one filter any of the ancestors? Given that this is a many-to-many relationship, when setting up a view, I can doing it through the in between object, but the properties I need to filter are on the ancestor.

Artists <-> Artists2Products <-> Products

Is there no way to get all Products that are for sale for a given artist? As opposed to just all Products of an Artist?

#591273

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Darcy

The issue certainly isn't unique, but it is a limitation that arises from how post relationships are currently implemented combined with using WP_Query under-the-hood to query posts.

For the particular case you describe you can try the following (I haven't tested it, so let me know how you get on).

You have your View which returns artist2product posts that are children of the currently displayed Artist which you use to display connected Product posts.

That View will return all such posts and you cannot filter it by the for-sale field on the parent Product.

But you can use the same id="$product" technique to instruct the Views shortcodes to output content from the parent Product post to conditionally output fields only if the parent Product for-sale field is the required value.

So you will loop over all of the Product posts for the current artist, but only output the content of that Product post if it has the relevant for-sale field marked.

Does that make sense?

If you need help with it let me know.

#591379

Cool. I understand this and have started to implement something like this.

However, I really want to manipulate the results before looping, so that I can sort as well as filter. Is this something that is being fixed in the new version?

#591430

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Darcy

I have added a comment to the feature request to ask that they ensure sorting as well as filtering by parent fields is included. If we can add one, we should be able to add the other.

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