0

I have created a custom taxonomy state and city. I want to fetch all the city for a given state using a drop down list. suppose first i need to select a state and then drop down automatically calculate the list of cities available for given state. This is really urgent for me.

Thanks, Sumit Jangir

1 Answer 1

0

You'll need to add the cities as a child of a state and then expose your custom taxonomies using the rest API.

After exposing it, you'll need to fetch the cities based on the selected state.

Please let me know if you need more information.

7
  • Can you please help me more on how to add cities as a child of a state. i have 2 custom taxonomy one for state and one for city. a simple example will help me to understand.i have a filter in front end where i first select state as a drop down and then city which should populate based on a state. Dec 15, 2020 at 16:56
  • Hello Sumit, adding the cities as "children" of the state is done via the hierarchical flag when you create the taxonomies of state and city....the default for hierarchical when you register taxonomies (and custom post types) is false, but you can change that to "true", then you can choose the state as the 'parent' taxonomy as you add cities taxonomies. Then when you query, you can choose to query for all children of the selected parent taxonomy. More info here: developer.wordpress.org/reference/functions/register_taxonomy
    – Trisha
    Dec 15, 2020 at 18:51
  • thx Trisha. i have already added state as a parent for city taxonomy but seems i need more info about how to query for all children of selected parent taxonomy and where to write code. $labels = array( 'name' => 'Cities', 'singular_name' => 'City', 'search_items' => 'Search Cities', 'popular_items' => 'Popular Cities', 'all_items' => 'All Cities', 'parent_item' => __( 'State' ), Dec 15, 2020 at 19:36
  • You'll need to build the <option> items inside your <select> element. Then, use an ajax request sending the id, to the rest-api (assuming that you already wrote the code to expose the Cities taxonomy content as an endpoint which receives the state id and returns all the child cities. This post may help Dec 16, 2020 at 20:30
  • Thanks Rafael. I got it . Dec 18, 2020 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.