1

This is my code which generates locations:

<select name="filter-location" id="location">
    <option value="">
        <?php if ( $input_titles == 'placeholders' ) : ?>
        <?php echo __( 'Location', 'realia' ); ?>
        <?php else: ?>
        <?php echo __( 'All locations', 'realia' ); ?>
        <?php endif; ?>
    </option>
    <?php
        $locations = get_terms('locations', array(
            'hide_empty'    => false,
            'parent'        => 0
        ));
    ?>
    <?php if ( is_array( $locations ) ) : ?>
    <?php foreach ( $locations as $location ) : ?>
    <option value="
        <?php echo esc_attr( $location->term_id ); ?>" 
        <?php if ( ! empty( $_GET['filter-location'] ) && $_GET['filter-location'] == $location->term_id ) : ?>selected="selected"
        <?php endif; ?>>
        <?php echo esc_html( $location->name ); ?>
    </option>
    <?php endforeach; ?>
    <?php endif; ?>
</select>

Locations

And this is the code which generates districts:

<?php $sublocations = get_terms('locations', array(
                                'hide_empty' => false,
                                'parent' => $location->term_id,
                               ));
?>

Districts

As you can see, to generate districts, it needs $location->term_id.

How can I send $location->term_id whenever someone selects a location?

  • you need ajax – David Lee Feb 18 '17 at 14:41
  • yes i know but my problem is how – Tornike Feb 18 '17 at 15:43
  • can you update your question stating that you are doing it with Ajax and code of what you accomplished so far? – David Lee Feb 18 '17 at 15:47
  • So you need to generate a option field based on the selected value of another option field, right ? – CodeMascot Feb 18 '17 at 17:01

Your Answer

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

Browse other questions tagged or ask your own question.