Skip Navigation

[Resolved] How to filter posts by related custom post type taxonomy

This support ticket is created 3 years, 11 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 10 replies, has 3 voices.

Last updated by Shane 3 years, 10 months ago.

Assisted by: Shane.

Author
Posts
#1600659

Dear Toolset team,

I need to filter posts in my template by a taxonomy of a related custom post type. Here is what I mean:
We have custom post type Events (Събития). We also created another custom post type Venues (Зали). There is a relationship created between those two CPTs. There is a taxonomy "Venue type" created in the Venues CPT. One of the taxonomy terms is Concert complex (Концертен комплекс) with id=41.
We also created a page template (Концерти) with a calendar in it. You can take a look at it here:
hidden link
At the moment calendar displays all the events available. What we want to achieve is to display events that are assigned only to a venue with that taxonomy term (id=41). In other words, we want to display only the events that will take place in the Concert complex.

We managed to filter events by their own taxonomy (Event Organizer) here:
hidden link
It was easy, but now we are stuck.

Can you please provide help on this topic?

#1601575
query-filter.png

Hello, based on your explanation I understand you want to filter a list of Event posts by the taxonomy terms assigned to their related Venue posts. Unfortunately there is not an easy way to do this from wp-admin. In the View editor, you are limited to filtering by the terms applied to the post type being queried. However, it is possible to do something like this with a nested View using an API query. Here is the process:
- Create a View of Venue posts filtered by taxonomy term ID. You can use a shortcode attribute here to pass in a variable taxonomy term ID or IDs, or you can choose a specific Venue Type here if you don't need this flexibility. In my example below, I am assuming you want to use a shortcode attribute like wpvvenuetype so you can reuse these Views elsewhere to display Events filtered by some other Venue Type.
- Use the Loop Wizard to create a loop using the "List with separators" format. Insert the post ID in the loop and use the comma , as a separator. The idea here is to create a View that will produce a list of Venue IDs like 1,2,3,4. This list of Venues is filtered by taxonomy term ID.
- Create a View of Event posts filtered by post relationship, where the related Venue post is set by a shortcode attribute wpvrelatedto. See query-filter.png for an example of this post relationship Query Filter.
- Use a shortcode to place the View of Event posts on a custom page. Use the View of Venue posts in a shortcode attribute to pass all the Venue IDs into the View of Events, like so:

[wpv-view name="Your Events View" wpvrelatedto="[wpv-view name='Your Venues View' wpvvenuetype='41']"]

This will display a list of Events filtered by related Venues in a specific Venue Type. Let me know if you have questions about this.

#1601703

Hey Christian,

Thank you for your feedback! The knowledge you shared is great, but I'm afraid I haven't explained my needs clearly.

You got it right that we need to filter a list of Event posts by the taxonomy terms assigned to their related Venue posts. From there, though, we want to use WP_Query to use those posts in our calendar here:
hidden link
(At the moment calendar displays all the events).
So, we need to create a query with custom post types Events that are related to a custom post type Venue with id=41.

Can you please modify the suggested solution to help us reach the goal? We highly appreciate your help!

Thank you in advance!

#1602893

So, we need to create a query with custom post types Events that are related to a custom post type Venue with id=41.
Okay so the taxonomy isn't involved in the Query at all. In this case, you can use WP_Query with the toolset_relationships arguments as shown here to fetch related posts: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

#1611715

Dear Christian,

I'm not sure that I explained it well. If so - please accept my sincere apologies! It's probably due to my English.

We have two custom post types - Events and Venues. There is a taxonomy for Venues that categorizes them by some criteria, let's say their size. So if we have the following examples:
- Berlin Arena - big size;
- London Hall - small size;
- Madrid Paraninfo - small size;
- Rome Colosseo - big size;
- Paris Stadium - medium size.
So let's imagine we need to fetch the events that will take place at big venues (Berlin Hall and Rome Colosseo). How can we achieve that?

Please accept my apologies once again for misleading you. Thank you in advance for your help!

#1614495

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nelly,

I've picked up this ticket from Christian since he was out for a few days.

Based on the thread so far the solution would be closely related to what Christian has provided in this thread here.
https://toolset.com/forums/topic/how-to-filter-posts-by-related-custom-post-type-taxonomy/#post-1601575

Can you let me know if this will be a dynamic filter or will you set a static filter.

Dynamic meaning you will allow your customers to have a checkbox that allows them to filter for "Big, small, medium etc".

Please let me know and we can take it from there.

Thanks,
Shane

#1614497

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nelly,

I've picked up this ticket from Christian since he was out for a few days.

Based on the thread so far the solution would be closely related to what Christian has provided in this thread here.
https://toolset.com/forums/topic/how-to-filter-posts-by-related-custom-post-type-taxonomy/#post-1601575

Can you let me know if this will be a dynamic filter or will you set a static filter.

Dynamic meaning you will allow your customers to have a checkbox that allows them to filter for "Big, small, medium etc".

Please let me know and we can take it from there.

Thanks,
Shane

#1614807

Hi Shane,

Thank you for your reply! We don't need a dynamic filter, a static one is perfectly fine.

Thanks,
Nelly

#1615777

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Screenshot 2020-05-07 at 3.33.18 PM.png
Screenshot 2020-05-07 at 3.31.18 PM.png
Screenshot 2020-05-07 at 3.30.10 PM.png

Hi Nelly,

Ok so what you need to do is the following :

1. Create a view for Venue and add a static filter to that view by clicking on the Query filter button in your view. See Screenshot.
2. Once you click the Add a Filter button you will need to add the filter for your taxonomy and ensure it is set to "Any of the following" and then select the relevant taxonomy. See Screenshot
3. Ensure that you save this view and then create another view for your Events. On the events view you are going to set a relationship filter by going to Add a Filter and then add the relationship filter.
4. Set the relationship filter to "The current post in the loop" see screenshot
5. Populate your Events view with the data that you want.
6. Go back to your Venue view and then use the Views and Fields button to add this Events view into the Venue view.
7. This should allow your events view to be filtered by the related taxonomy from Venues.

This is similar to the method that christian illustrated in his post but a slightly different approach. Either method will work.

Please let me know if this is clear or if further guidance is needed.

Thanks,
Shane

#1631773

Hi Shane,

Thank you very much for your help. The method you described works but we still need assistance. I'm afraid we have to take a different approach. We use WP_Query to fetch the events and show them in the calendar. Please, take a look here:
hidden link
Unfortunate we do not know how to filter the events by the related post taxonomy. At the moment we display all the events but we have to eliminate the events that do not happen in a certain venue.
So the question is how to set the WP_Query to "insert" the events in the calendar. I will appreciate your further help!
Thank you in advance!

#1632341

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nelly,

If you really need to filter this by the related taxonomy then I won't be able to provide much assistance as this will require some amount of custom code as you say and custom code like this would be out of the scope of our support forum.

Thanks,
Shane

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