Site icon Hip-Hop Website Design and Development

Dropdown Select Post Filter

I would like to display the posts when I click on an item with the dropdown select

Currently, my select is OK, all my terms are displayed and all posts too.

I just want to know of it’s possible to filter

This is my dropdown select :

<select name="soins-taxonomy">
    <option value="all">Tout afficher</option>

    <?php
    // Get the taxonomy's terms
    $terms = get_terms(
        array(
            'taxonomy'   => 'location',
            'hide_empty' => false,
            'exclude' => 1
        )
    );

    // Check if any term exists
    if ( ! empty( $terms ) && is_array( $terms ) ) {
        // Run a loop and print them all
        foreach ( $terms as $term ) { ?>
            <?php echo '<option value="' . $term->term_id . '">' . $term->name . '</option>';
        }
    } 
    ?>
</select>

And this is my Query to display posts :

<?php

$ourCurrentPage = get_query_var('paged');

$args = array(
    'order' => 'ASC',
    'post_type' =>  'etablissements',
    'taxonomy' => 'location',
    'posts_per_page' => 9,
    'paged' => $ourCurrentPage
);

// Custom query.
$query_loc = new WP_Query( $args );

// Check that we have query results.
if ( $query_loc->have_posts() ) {

    // Start looping over the query results.
    while ( $query_loc->have_posts() ) {

        $query_loc->the_post();?>


        <!-- START ARTICLE -->
        <div class="col-xl-4 col-lg-6 col-md-6 mb-30">
            <div class="single-etablissement">
                <p class="single-etablissement-title"><?php the_title(); ?></p>
                <p><?php the_field('eta_adress'); ?></p>
                <p><?php the_field('eta_cp'); ?> - <?php the_field('eta_city'); ?></p>
                <p><?php the_field('eta_country'); ?></p>
            </div>
        </div>
        <!-- END ARTICLE -->

<?php

    } // End while   

} // End if

else { echo '<p>Aucune actualité trouvée</p>'; } ?>

<?php wp_reset_postdata(); ?>