0

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(); ?>

2 Answers 2

0

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.

0
 <?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 :)

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jan 4, 2022 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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