Home › Toolset Professional Support › [Resolved] Building related dropdown selection
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 |
---|---|---|---|---|---|---|
- | - | 14:00 – 20:00 | 14:00 – 20:00 | 14:00 – 20:00 | 14:00 – 20:00 | 14:00 – 20:00 |
- | - | - | - | - | - | - |
Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)
Tagged: Post-relationship
This topic contains 14 replies, has 2 voices.
Last updated by Beda 5 years, 8 months ago.
Assisted by: Beda.
Dear Sir/Madam,
I want to build a related dropdown selection, I have two custom posts, Location and District. The relationship between Location and District is 1-to-many, one location will link up to many districts. I have already created the custom posts and made the relationship. How can I make two dropdown selections, when I change the location then the selection from the district will be filtered and only show the related district for selection?
Best regards,
Kelvin.
You mean in a View Custom Search? Or where do you want to implement these dropdown selectors?
You can filter by parents in Views, however you cannot filter by the same post as well, so that means you'll either filter Locations by Districts or Districts by Locations, but not both in the same filter, as there is no 3rd post type child to either of both
Dear Beda,
I want to make it in View Custom Search but if it can't, then want to implement these dropdown selectors.
What do you mean I can filter by parents in Views, however I cannot filter by the same post as well?
I want to put a search at the home page, customer can firstly select the Location and then the options list for District will be filtered according to the Location selected. Location is the parent of the District.
best regards,
Kelvin
I understand.
This is only partially possible with Toolset.
You will always see the selectors, and they always will be populated with options. What you can do however is to make one dependant on the other, for example in parent post-relationship queries, you can demand the user to enter a parent, and a grandparent in a second select Field.
But those select fields will always show and always be populated by all options, just, one of them won't be active until an option from the first one is chosen.
That's the only "dependant" select Field search you could build with Toolset - and, you can update the Search inputs on the fly with only options that would produce valid results - but only once you select at least one of the search items in at least one filter.
If you want I can set up a rough draft as an example for you to see directly on the website.
For this, I'd need access and permission to create new pages and views.
I can then set up an example for you if I see a possible approach to the goal on the site.
Please explicitly mention whether or not I'd be allowed to make changes and if, what kind of changes.
I'll then set up an example.
Not any 2 post Types have a common attribute, depending searches only function in specific parent/child relations.
You did not mention if I am allowed to make an example on the site, so I refrained from it.
However, you'd create a new View, in the View choose to display the Child Post (Districts) and then add a Query Filter by its parent (Locations).
Then, in the front end, you'd be able to show Districts by filtering them by Locations.
But you cannot filter by both Locations and Districts, because there is no other end to display.
You can always only display one end, and query by the other end(s).
So for example, if you'd want to display some posts, and filter those posts by Districts AND Locations, then you'd need to add a child to (in your case) Districts. Again for one District could be related to many other posts of another type.
Then, you could query that post type, and add a filter for both the parent and grandparent (District and Location).
I hope the mechanisms of the queries remain clear with above, please let me know otherwise so I may elaborate more.
Right now, with the setup you have you can create lists of Districts filtered by Locations or Locations filtered by Districts, but not by both.
Now if you have a chain of parents (grandparent, parent, child) then you can query the child for both parent and grandparent.
I can set up some examples, inclusive post types if you agree, but maybe you prefer me doing this on a staging server?
I reactivated the private reply in case you need to submit other access details.
Dear Beda,
Yes, of course, I would thanks if you can make a sample. You can access the site using the private message.
With the data you have now, you can do this:
1. Show Districts and let users search by Locations:
hidden link
Insert the View "Districts filtered by Locations" here to see it in action:
hidden link / hidden link
2. Show Locations and let users search by Districts:
hidden link
Insert the View "Locations filtered by Districts" into the page we used above as an example to see it in action
Note that here, we inserted a second View to the Loop, displaying the many related Districts, since that is not a unique end of the relationship we need to nest another View to display the related results as well.
3. You cannot have a page where you can search by both District and Locations at the same time because well, there would be nothing to display since there is no third Post type related to both the Location and District.
Please let me know if anything remains unclear.
Dear Beda,
Thanks for your reply. It looks a bit complicated and I need time to understand it.
From your sample, hidden link, should it be two dropdown selections, firstly select the Location and then having the second dropdown list District?
Best regards,
Kelvin.
You cannot have a page where you can search by both District and Locations at the same time because well, there would be nothing to display since there is no third Post type related to both the Location and District.
So, the example hidden link should not/can not have 2 dropdowns.
Again, what would you display as results if you already filter by both Post Types? There would be nothing lef to display.
Dear Beda,
I think my message misleading you. I have a custom post "Estate" which contain the field "District", I create the custom posts "Location" and "District" because I will add new to both later via a CRED form.
Location is the parent and District is the child relationship.
I want to make a search form with two dropdowns "Location" and "District". By default, both dropdowns contain all option in select. Visitor can either directly select the District and search or firstly select the Location and then the options from District will be filtered, after selecting the district and search, all Estates in the same District will be searched.
Both Location and District will just are searching parameter but not the searching posts.
Hope my description can make you more clearly what I want to do.
Best regards,
Kelvin
Thank you for clarifying, please allow me to elaborate below a few details that are important:
1. You have one relationship. This is a relationship where One Location can have Many Districts connected to it. See hidden link.
1 Location can be assigned to one District. Infinite Districts can be assigned to one Location.
Hence you can either query Districts by Locations or Locations by Districts but you cannot query anything else by that data, since it's not connected to any other post type.
2. You can hence not create a Search form with both dropdowns "Location" and "District" - As elaborated previously you need to filter some results by some values and if you use both Location and District as a filter, nothing is left over to display a result.
The only 2 filter methods possible are the ones I created on your site as examples. This is not any different in a PHP or MySQL query.
3. To filter Estates by Districts and Locations, you need to create a relationship from Districts and Locations to Estates.
Otherwise, you cannot use those "relationship" filters, as there's nothing to display since those post types are not related.
4. The custom field you mention is just a Select Field holding some values, that does not relate those Posts in any way to Districts or Locations.
5. You cannot filter Estates by Location or District posts because well, there is no relation between those, no reference so to speak.
To solve this, if you want to display Estates, and filter them by Location and/or District, you'd need to create one more relationship between the District and Estates.
I assume, that one Estate only can be in One District, but one district can, of course, have many estates.
Since Districts are always in one Location, according to your relationship as it's set up, you'd then have a grandparent (location), a parent (district) and a child (Estate).
Hence then you'd create a View to query estates, and add a query filter by the parent and grandparent, both in the same filter.
This will then allow you to filter the estates by its location and district.
You'd, however, need to set up a new Post Relationship One To Many between Districts and Estates.
Please let me know if you'd need an example View after you create the relationship and connect some posts.
Dear Beda,
Thanks for your advice. I finished creating a relationship and connect some posts. This is the search page I build (hidden link with backend URL: hidden link)
In order to let you know the selection in English, I add all the slug of the location, district after the title.
Here is the relationship
Location 1-M District
District 1-M Estate
Estate 1-M Design
Below are the posts I prepared in order of post / location / district / estate
Elegant Living Room (post id 536) / nt / shatin / city-one-shatin
Stylish Living Room (post id 523) / nt / shatin / city-one-shatin
Fashion Cloakroom (post id 522) / nt / shatin / peak-one
Simple Kitchen (post id 513) / hk / taikoo / taikoo-shing
What I want to do, when a visitor changes the location, the only district belong to the location will be listed, if visitor changes the district, the only estate belongs to the district will be listed.
If possible, I also want to update the estate field immediately if the location is changed.
Best regards,
Kelvin.
I see only 2 relationships so far:
hidden link
Locations Districts and Districts Estates
Hence the search only can be for those.
Listing the most child post (Estates) and querying by it's parent District and GrandParent Location. See also the screenshot for that, and the view here hidden link
Note that to make the inputs dynamic I chose "Let me choose individual settings manually" in the Custom Search Settings.
Especially interesting for you here is the setting "Show only available options for each input".
As you can see it's one greyed out until you choose the first option hidden link, and it follows the expected grandparent/parent/child hierarchy.
Is that what you need?
Dear Beda,
Yes, there is no relationship between the designer and estate, district and estate is 1-M relationship.
I find you add the Query Filter "in the Districts Estates relationship", I also want to add the filter "in the Locations Districts relationship" but don't know how to add it. Regards to the code
[wpv-control-post-relationship ancestors="location@location-district.parent>district@district-estate.parent" url_param="wpv-relationship-filter"]
How can I make it? Could you screen capture?
You can add only one relationship filter tree a time each view.
That means, you can either list all Estates and filter them by parent and grandparent (this is what I did in the example), or you can filter any other post type in this relation by its next relative.
So, for example, you can filter the Parent Districts by Estates, OR by Locations, but not both at the same time, in one View.
OR, you can filter Locations by its child Districts,
I have added several Views as examples for you.
Note, whenever the GUI greys out the buttons it means you cannot re-use the same filter or option as its already used, and manually copy-pasting a new version of the filter won't work.
If you want to apply more than one relationship filter to the same view you'd need to use the Views API Filters.
Or, you can nest Views into other Views. More on that later, here the example Views of what you can do with the native features:
1. List Estates filtered by District and Location: hidden link
2. Locations filtered by District and Estate: hidden link
3. Districts by Locations: hidden link
4. Districts by Estates: hidden link
Bottom line, you could say one View can always query in one DIRECTION of the relationship, infinitely, but not in both directions.
So you can query any child by any of its parents in a direction but you cannot query a post type by both a child and a parent type.
Related the Views API I mentioned earlier, it is possible to filter the Views results with several hooks and add other query arguments - but that does not allow you to produce front end inputs where the user can choose and send in the query, instead, it happens in the background, without the user's interaction.
What you could do to solve this is creating some custom inputs the user will choose and generate URL parameters when submitting the search from those inputs.
Then you could listen to those URL parameters and pass them to your Custom Code query.
You could then hook these custom query arguments to the wpv_filter_query Views API:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
That will ensure your new custom query arguments apply to to the query generating the View's results.
We can however only help with some examples in this case, if you need more elaborated help with Custom Code you could contact a Contractor https://toolset.com/contractors/
Examples on how to alter the Views Query which can help to achieve the goal can be found in the DOC itself ("More" section), and here:
hidden link
Other related code: hidden link
What you can also do is nest Views.
Let's say you create a View that lists the Districts with a Custom Search by Location, you create another view where you list Estates with a Query Filter by "parent is set in the parent view loop". Then you insert this second view in the first View's loop and that first View in a page.
On the page now you will see a list of Districts filterable by Location and below each District its related Estates.
Similarly, you can display related data, so for example in a View listing Estates you can show in the very loop the parent District, by using (you can set this in the GUI when inserting ShortCodes) the "item" attribute pointing to the post you want to display.
A search input for the same relationship in the opposite direction is, as above shown, not something you can add native to Views, without some custom work.