Dropdown Select Post Filter

时间:2019-04-18 作者:WDCreativ

我想在单击下拉选择项时显示帖子

目前,我的选择是确定的,显示所有我的条款和所有帖子。

我只是想知道是否可以过滤

这是我的下拉选择:

<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>
这是我用来显示帖子的查询:

<?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 个回复
SO网友:Qaisar Feroz

您可以使用tax_query 对于Taxonomy Parameters 为您的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
);
我希望这会有所帮助。

SO网友:MR. shaikh__92

 <?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;
也许对你有帮助:)

相关推荐

“POSTS_WHERE”筛选器未在`wp_ajax`中应用“WP_Query”

我有一个函数,它添加了posts_join 和aposts_where 在中搜索WP_Query. 它在普通的php呈现的搜索页面上运行良好。然而,我也将其用于ajax产品搜索和其他posts_join 和aposts_where 似乎不适用。以下是连接和位置:function search_by_meta_join($join) { global $wpdb; if (is_search()) { $join .= \" LEFT JOI