Skip Navigation

[Resolved] Custom Search for Many To Many Relationships

This support ticket is created 5 years, 10 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 7 replies, has 2 voices.

Last updated by Christian Cox 5 years, 9 months ago.

Assigned support staff: Christian Cox.

Author
Posts
#508422

Hello,

I created a mamy to many relationship between Woocommerce Products and Vehicles (CPT), trought a conector named "Values" (CPT). Now im trying to create a custom search, i want the user to be able to find products using Vehicles Information. But i dont know how to achieve that.

I created custom searches with views plugin, but i cannot find the custom fields or custom taxonomies to search in when im creating the custom search in views. I tried selecting as content Products and Values, but not results.

Can you please tell me what do i have to do? or what am i doing wrong?

Regards

#508655

Hi,
I would start by setting up a custom search of Vehicles, and include all the criteria you would like to search on - taxonomy, custom post fields, etc. This should give you results for each matching Vehicle. Then, I would create a View that shows all the Values that are children of the current Vehicle in the loop. In the output of this loop, include information about the parent Product.

Embed the second loop in the output of the first loop, and you will end up with a list of search results like:

Vehicle 1
- Product 1
- Product 2

Vehicle 2
- Product 2
- Product 3

Vehicle 3
- Product 1
- Product 2

...and so on. If you don't want to display the Vehicle information, only the Products, you can leave the Vehicle name out of the loop output of your first loop. Then you end up with only a list of Products, but you may have some duplicate results.

Here's the documentation we have for displaying many-to-many relationships: https://toolset.com/documentation/toolset-training-course/part-9-many-to-many-relationships-in-toolset/

The key is understanding how to use parent syntax:

[wpv-post-link id="$product"]
#509874

Ok, i understand but is there an option to create a custom search that allows the user to search product info and vehicles info in the same custom search?

Is there a tutorial to learn about using parent syntax? i kinda understand how it works but i dont know how to use it really.

Thanks for your help Christian

#509939

Here's some more information about the parent post syntax:
https://toolset.com/documentation/user-guides/displaying-fields-of-parent-pages/

Types currently gives you the ability to select a specific ancestor, then filter child posts within that ancestor's children. However we do not currently offer a way to search for child posts filtered by parent post's custom fields or taxonomy associations. Here's some more information about the ancestor filter control item:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-control-item

#509944

So maybe the structure doesn't fit my needs. I need to search products according to Product criterias and vehicles, vehicles and products are different but they need to be connected in some way, would you propose a different way to achieve this?

#509953

Does it make sense to include Vehicle information as a taxonomy for Products? I'm not sure what your Vehicle information contains, but it may be possible to create taxonomy terms for each criteria, and apply those to each Product.

So say your Vehicle criteria are:
- Sedan
- Coupe
- Truck
- Hybrid
- Electric
- New
- Used

These could each be taxonomy terms, and you would apply those terms as you would categories or tags whenever you create a Product. Then when you create your Product custom search, you can let users choose the terms they want to search for. If this doesn't sound like what you need to accomplish, I would need some more information about Vehicles, what criteria you want to search by, the type of data for each criteria, etc.

#509968

We already did that but because one product fit serveral vehicles the data base got too huge and searches too slow, we have arround 80.000 different products and there are almost 7.000 different vehicles.

Thats the reason i thought M2M relationship could be a better and efficient solution.

An other way could be this: We have a code that we use to group similar products ("Equivalence"), is it possible to use this code instead of "Values" to connect products and vehicles, also use custom fields to fill information about the product in this CPT ("Equivalence").

This structure could allow me to create a custom search with product info and vehicles info?

I hope i've made my self clear

#510377

I think I follow you. Let's say you have "Equivalence" custom field applied to both Vehicles and Products. There are 3 options for this field.

Equivalence field
- EQ1 option
- EQ2 option
- EQ3 option

Each Vehicle can have one or more Equivalence selected. Each Product can have one or more Equivalence selected. You would like to create a search that displays Products, but allows the user to filter based on both Vehicle Equivalence and Product Equivalence. Your controls would look something like this:

Vehicle: (Select Equivalence)
Product: (Select Equivalence)
Search

In order for this to work, you must manage equivalences for BOTH Vehicles and Products in each Product. You would not even need to set an Equivalence for each Vehicle unless you need it for other reasons. Instead, you would have two custom fields in Products, "Vehicle Equivalence" and "Product Equivalence". Then any View of Products can access both of those fields for searching and filtering.