Skip Navigation

[Resolved] Restrict City Options Based on State Selected (Similar to WPDiscover RealEstate)

This thread is resolved. Here is a description of the problem and solution.

Problem: I am using the Real Estate reference site as an example. When creating a House post, I would like to select a State, then select a City from a list of Cities in that State.

Solution:
In wp-admin, there's no built-in way to do this. There is no "grandparent" post selection in post relationships, only parent selection. You could add the state abbreviation to each duplicate city post title:
City - "Kansas City, KS"
City - "Kansas City, MO"

In Forms, there is a more complex solution:

- Create a generic select field that includes an option for each State. The value of each option should be the State post ID. You can use a View of States to create the options for the generic field.

- Next create one generic select field for each State. Add options for each City in that State. You can use a View of Cities filtered by post relationship to create the options for the generic fields.

- Use Conditional Field Groups to show and hide the City select fields based on the selected State.

- Then capture the selected City post ID using the Forms API, and use the Post Relationships API to link the House and the City.

Relevant Documentation:
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 6 years 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 5 replies, has 2 voices.

Last updated by hui-suanC 6 years ago.

Assisted by: Christian Cox.

Author
Posts
#1139457

In the current WP Discover Real Estate site, each House has a relationship with City,

These are the relationships that are there:

State - City
City - House

But there is no

"State - House"

This works out fine if there are no duplicate City names from different states, but what if there is?

e.g. Kansas City from Kansas, and Kansas City from Missouri?

Hence when creating a new house/apartment, I would like to be able to select a State, and from the value of the state, restrict the possible Cities (post relationships) to only the cities in that state. Is this possible?

#1139836

In wp-admin, there's no built-in way to do this. There is no "grandparent" post selection in post relationships, only parent selection. You could add the state abbreviation to each duplicate city post title:
City - "Kansas City, KS"
City - "Kansas City, MO"

In Forms, there is a more complex solution:
- Create a generic select field that includes an option for each State. The value of each option should be the State post ID. You can use a View of States to create the options for the generic field.
- Next create one generic select field for each State. Add options for each City in that State. You can use a View of Cities filtered by post relationship to create the options for the generic fields.
- Use Conditional Field Groups to show and hide the City select fields based on the selected State.
- Then capture the selected City post ID using the Forms API, and use the Post Relationships API to link the House and the City.

Let me know if you have questions about either approach.

#1140163

Thanks, that was a very detailed way of solving the problem. My main gripe with using Select fields was that until now, inputting the values has been very time consuming, and I know I'm not the only one who feels this way.

https://toolset.com/forums/topic/how-to-import-states-and-countries-into-selects-for-fields/
https://toolset.com/forums/topic/add-30-options-to-a-dropdown-custom-field-in-a-group-any-way-to-copy-paste/

#1140173

- Use Conditional Field Groups to show and hide the City select fields based on the selected State.

>> Btw I wanted to ask this: I tried using Conditional Field Groups but it only affects whether the field is shown or hidden as a whole, not what options are available in it. Am I wrong?

#1140395

I tried using Conditional Field Groups but it only affects whether the field is shown or hidden as a whole, not what options are available in it. Am I wrong?
That's correct. In the example I was describing, there are at least 51 select fields. 1 select field contains all the States. Then there are 50 more select fields, each containing all the Cities in one State.

#1140938

I understand. That is a lot of work but a straightforward solution.