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.
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:
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!
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!
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.
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!
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.