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 'pAucune actualité trouvée/p'; } ?

?php wp_reset_postdata(); ?

Topic dropdown filters posts Wordpress

Category Web


 <?php $location = isset($_GET['location']) ? $_GET['location'] : '';?>

<form action="" method="GET" >
                <select name="location" id="location" onchange="submit();">
                <option value="" <?php echo ($location == 'location') ? ' selected="selected"' : 'location'; ?>>Show all</option>
                <?php 
                    $locations = get_categories('taxonomy=location&post_type=etablissements'); 
                    foreach ($locations as $location) : 
                    echo '<option value="'.$location->slug.'"';
                    echo ($location == ''.$location->slug.'') ? ' selected="selected"' : '';
                    echo '>'.$location->slug.'</option>';
                    endforeach; 
                ?>
                </select>
</form>
$taxs = array();
 if( isset($_GET['location']) && '' != $_GET['location']) {
$taxs[] = array(
        'taxonomy' =>'location',
        'field' => 'slug',
        'terms' => $location
        
);
}
$args['tax_query'] = $taxs;

may be it's help you :)


You can use tax_query for Taxonomy Parameters for your WP_Query.

$term_id = $_REQUEST ['soins-taxonomy'];  // or use $_POST or $_GET as the case may be

$args = array(
    'order' => 'ASC',
    'post_type' =>  'etablissements',


    'tax_query' => array(
        array(
            'taxonomy' => 'location',
            'field'    => 'term_id',
            'terms'    => $term_id,
      ),


    'posts_per_page' => 9,
    'paged' => $ourCurrentPage
);

I hope this may help.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.