[Resolved] Filtering (custom search) in child CPT by parent (WP post) taxonomy
This thread is resolved. Here is a description of the problem and solution.
Problem: I have two custom post types in a parent / child relationship. They are Restaurant (parent) and Location (child). I would like to show a custom search View that filters by the taxonomies applied to the parent posts, and I would like to show the matching Location results on a Map.
Solution: Create a View of Locations, filtered by post relationship, where the parent post is set by the current post in the Loop. In the Loop of the View, insert the Map Marker shortcode.
Create a View of Restaurants, filtered by the various taxonomies you want to show in a custom search. In the Loop Editor, insert the View of Locations in the wpv-loop tags. Add the Map shortcode outside the wpv-loop tags.
0% of people find this useful.
This support ticket is created 6 years, 4 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.
Our next available supporter will start replying to tickets in about 4.77 hours from now. Thank you for your understanding.
I am trying to: Set up custom search to filter a child CPT by parent (WP post type) taxonomy
Link to a page where the issue can be seen: hidden link
I expected to see: Lots of money in my bank account.
Instead, I got: But alas, I have none 🙂
I know this wasn't possible a year or more ago but with the recent advancements in relationships in Types I thought it was worth making the inquiry again.
I have will just use a real-life example. Restaurants are WP post types. Restaurant locations (a CPT we call Map Markers) are a child CPT of Posts (Restaurants). So restaurants have multiple locations, others have single locations (Map Markers). Of course, I want to create pages and posts that show lists of restaurants and maps that include map markers (locations) for these restaurants.
I want to be able to filter the restaurants by Type, Cuisine, Rating, and Price. I have created taxonomies for those as that is the most logical way of create that type of data (and taxonomies are more readily translated to foreign languages). Sounds logical, right?
I would prefer to be able to display this map in a WP page (rather than on a Restaurant post).
What works now (sort of). If I show the taxonomies on both Posts (Restaurants) and Map Markers (restaurant locations) I can then query the Map Markers (the "many" side of the one to many relationship) by the taxonomy and link to the parent. I can even DISPLAY the taxonomies of the parent. For simplicity, it would be easier to query the map markers BY THE PARENT TAXONOMY rather than the content of the taxonomy in the child location. This would prevent having to input the taxonomy in both the parent and the child and would reduce the amount of error of data input.
This was not previously possible, right?
With advancements in your relationships in more recent versions, has anything changed? What are my options?
Supplement: I have yet to establish and update my existing data on this example site to the new RELATIONSHIPS. It looks as if the desired result might now be possible by updating to RELATIONSHIPS (it's activated but I haven't set up any relationship yet. I still have the prior parent-child relationships in place. I note that I have hundreds of posts using taxonomies rather than custom fields. It would be a huge effort to duplicate (convert) existing taxonomies to "select" or "checkbox" custom fields and only two of those are really a good fit for doing that. I prefer to solve this with taxonomies if possible. Thanks.
Hi, I think this is possible now without much modification. You need not update to the new relationships system to do this, but you certainly may update if you want to take advantage of the new M2M relationship features. Here's how I would set it up.
- Create a View of Restaurant Locations, filtered by post relationship, where the post parent is the current post in the loop.
- In the Loop Output editor of this View, insert the Toolset Map Marker using the Fields and Views button. Do not output the map in this View, only the marker. During testing and debugging, you may find it useful to output the Post Title here as well, to confirm things are working as expected.
- Create a View of Restaurants, filtered by the various restaurant taxonomies.
- In the Loop Output editor of this View, insert your View from step 1. You may also want to output the Restaurant post title here during testing to confirm the filters are working as expected. Just after the wpv-layout-start shortcode, insert the Toolset Map using the Fields and Views button. Make sure the Map ID here matches the Map ID used when you created your markers in step 2.
Now you have a nested View structure. The outer View allows you to filter Restaurants by taxonomies. The inner View places all the corresponding child Locations on the map. It means you no longer need to add taxonomy terms to the child post for this map display, since you're filtering the Restaurant parent posts and not the child posts. Please let me know if I've misunderstood how it should work.
Woo. Hoo. My first nested view!
This, of course, worked. But I tried to add some embellishments. First I filtered the entire loop for the locations/markers (the inner view) to those in a specific neighborhood. Then I wanted to put a list of the results on the same page. Added some output to the OUTER loop displaying the Restaurant parent / WP post taxonomies. And, of course, I wanted to make it more difficult so I've attempted to output to a TABLE grid.
Question #1: I assume I still cannot use Bootstrap grids with Divi. Is that still the case? If so, is what I've done with a table grid the only (best) suitable alternative?
Question #2: When I use the code in the table grid wizard, the tags after the table are RED in the Editor (see uploaded image). Is there some errant bit of code here?
Question #3: Under this structure I'm getting a host of "No results found" being displayed from the INNER (locations/map markers) loop as the table groups are cycled. You can see lots of them above the GRID (after the filters and map) before you do any filtering. Why is this happening? Is the code that displays this "No results found" really necessary on a loop that only produces map markers? Won't the map just be empty if there are no results? Please clarify the best way to handle this. Can I merely remove that code from the inner (locations) loop?
Thanks! That nested view is NIFTY! 🙂
Question #1: I assume I still cannot use Bootstrap grids with Divi. Is that still the case? If so, is what I've done with a table grid the only (best) suitable alternative?
It's still the case that Divi is largely incompatible with Bootstrap. A table is probably the easiest alternative to implement because the rows and columns built-in, to a large extent. If you're comfortable writing custom CSS, you can probably make a more responsive design using media queries and percentage-based CSS widths applied to divs.
Question #2: When I use the code in the table grid wizard, the tags after the table are RED in the Editor (see uploaded image). Is there some errant bit of code here?
The code editor thinks that some code is misplaced because of how the table structure works with wpv-item, index and padding. The table row tag must be closed after the last item and after the pad-last item, so the syntax checker gets confused by the "extra" closing tr tag. Nothing to worry about here, it's expected for this type of Loop.
Question #3: Why is this happening? Is the code that displays this "No results found" really necessary on a loop that only produces map markers?
Is it possible some Restaurants have no Restaurant Locations? That would produce this type of result set. You can remove these default "No results found" messages from both Views to clean up the display. They are defined in each View's Loop Output editor near the bottom.