admin管理员组文章数量:1122846
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(); ?>
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(); ?>
Share
Improve this question
edited Apr 19, 2019 at 5:19
fuxia♦
107k38 gold badges255 silver badges459 bronze badges
asked Apr 18, 2019 at 15:55
WDCreativWDCreativ
32 silver badges6 bronze badges
2 Answers
Reset to default 0You 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.
<?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 :)
本文标签: Dropdown Select Post Filter
版权声明:本文标题:Dropdown Select Post Filter 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736281482a1926329.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论