K
K
Keymorfist2016-11-01 20:55:24
CMS
Keymorfist, 2016-11-01 20:55:24

How to get the term_id of the parent from get_term() and pass this value to child_of to another form?

Good afternoon everyone, please help me with the script.
There is a small form
5b93eb2fbcd3422783f50bee43d87619.jpg
AND a custom taxonomy "city"
ee1e491d32364786a7906115e22a2c03.jpg
It's the only one.

<select id="region">
  <?php 
    $city=get_terms( 'city', 'orderby=name&hide_empty=0&parent=25'); 
      foreach ($city as $term){ 
        echo "<option value='{$term->term_id}'>{$term->name}</option>";} ?>
</select>

<select id="city">
  <?php 
    $city=get_terms( 'city', 'orderby=name&hide_empty=0&child_of='); 
      foreach ($city as $term){ 
        echo "<option value='{$term->term_id}'>{$term->name}</option>";} ?>
</select>

My goal:
1. Select the parent category in the form
2. In the second form, all categories inside the parent are displayed
I just recently started to master php, please help me solve this problem. How can I get the term_id from the first form and pass this value to the child_of of the second form?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2016-11-02
@Keymorfist

The first select is loaded in the usual way, PHP generates a list of terms of one parent ("All cities"). Next, there should be ajax - on the 'change' event of the select of these terms, you send a request to admin-ajax.php, create a handler for the wp_ajax_{your_action} hook, to which you pass the ID of the selected term, and return a list of child terms.
PS: If this is on the frontend, then you need to hang up on 2 hooks - wp_ajax_{your_action} and wp_ajax_nopriv_{your_action}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question