Skip Navigation

[Closed] Filter view by taxonomies of posts in relationship

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.

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 7 months, 3 weeks ago.

Author
Posts
#2689056

I have 2 post types in a many to many relationship: Animals, Chemicals.

Chemicals has 2 custom taxonomies: Families, Uses

I'm creating a view to show Animals. I would like to have search filters that filter Animals by terms of their related Chemicals.

So if an Animal has a Chemical associated with it, I'd like to filter by the Chemicals taxonomies.

Is this possible and if so how?

#2689501

Nigel
Supporter

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

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

Hi there

Sorry for the slow response over the weekend.

What you are aiming for isn't ordinarily possible. Something can only be filtered by its own properties, not by properties of some other thing (even if there is a connection between the things).

It could—in theory—be possible to construct queries that worked in that way, but it's not something that is available with Toolset post relationships.

It needs some development work to implement, if that is something you are in a position to do (or organise).

The first part is that you would need to add a custom filter control to the search filters of your View, which would offer the available options from the Families and Uses taxonomies. The search section is a form which is submitted, and these custom controls would be fields that get submitted along with the form.

You could then use the Views API, namely the wpv_filter_query filter, to modify the base query of the View which will display Animals. You would get the values of the custom filter fields, if provided, query Chemical posts that have those terms assigned, then use the Relationships API function toolset_get_related_posts to find the Animal posts connected to those Chemical posts.

You would arrive at an array of Animal post IDs, and you could update the base query to limit the Animal posts to this array of IDs (passed via the post__in argument).

There's quite a lot to digest there.

If you try to implement such a solution and get stuck, let us know and we can try to help with the particular step you are having difficulty with.

https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters

The topic ‘[Closed] Filter view by taxonomies of posts in relationship’ is closed to new replies.