【问题标题】:Wordpress - Filtering Custom Post Type by multiple Custom Taxonomies on front endWordpress - 在前端通过多个自定义分类法过滤自定义帖子类型
【发布时间】:2023-04-10 00:14:01
【问题描述】:

我正在尝试允许用户从多个自定义分类中选择标准,以便通过多选的 from 在前端过滤自定义帖子类型。

我的问题是代码只有在每个选择都选择了标准时才有效。试图找到一种方法,让用户也可以只选择其中一个选项,它只会按该选项过滤 CPT。

* 所有代码都是抽象的。分类名称实际上是:incident_category、incident_classification、event_type 等。

标记:

<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
    <?php
        if( $terms = get_terms( array(
            'taxonomy' => 'tax1',
        ) ) ) :
            echo '<select name="tax1filter"><option value="">Select tax1...</option>';
            foreach ( $terms as $term ) :
                echo '<option value="' . $term->term_id . '">' . $term->name . '</option>';
            endforeach;
            echo '</select>';
        endif;

        if( $terms = get_terms( array(
            'taxonomy' => 'tax2',
        ) ) ) :
            echo '<select name="tax2filter"><option value="">Select tax2...</option>';
            foreach ( $terms as $term ) :
                echo '<option value="' . $term->term_id . '">' . $term->name . '</option>';
            endforeach;
            echo '</select>';
        endif;

        if( $terms = get_terms( array(
            'taxonomy' => 'tax3',
        ) ) ) :
            echo '<select name="tax3filter"><option value="">Select tax3...</option>';
            foreach ( $terms as $term ) :
                echo '<option value="' . $term->term_id . '">' . $term->name . '</option>';
            endforeach;
            echo '</select>';
        endif;
    ?>
    <button>Apply filter</button>
    <input type="hidden" name="action" value="myfilter">
</form>
<div id="loop"></div>

JS:

jQuery(function($){
    $('#filter').submit(function(){
        var filter = $('#filter');
        $.ajax({
            url:filter.attr('action'),
            data:filter.serialize(), // form data
            type:filter.attr('method'), // POST
            beforeSend:function(xhr){
                filter.find('button').text('Processing...'); // changing the button label
            },
            success:function(data){
                filter.find('button').text('Apply filter'); // changing the button label back
                $('#loop').html(data); // insert data
            }
        });
        return false;
    });
});

功能:

add_action('wp_ajax_myfilter', 'misha_filter_function'); // wp_ajax_{ACTION HERE}
add_action('wp_ajax_nopriv_myfilter', 'misha_filter_function');

function misha_filter_function(){
    $args = array(
        'post_type' => 'cpt',
        'orderby' => 'date',
        'order'    => 'DESC',
    );

    $relation = 'OR';

    if(isset($_POST['tax1filter']) && isset( $_POST['tax2filter'] ) && isset( $_POST['tax3filter'] )) {
        $relation = 'AND';
    }
    if( isset( $_POST['tax1filter'] ) ||  isset( $_POST['tax2filter'] || isset( $_POST['tax3filter']) )
        $args['tax_query'] = array(
            'relation' => $relation, 
            array(
                'taxonomy' => 'tax1',
                'field' => 'id',
                'terms' => $_POST['tax1filter']
            ),
            array(
                'taxonomy' => 'tax2',
                'field' => 'id',
                'terms' => $_POST['tax2filter'],
            ),
            array(
                'taxonomy' => 'tax3',
                'field' => 'id',
                'terms' => $_POST['tax3filter'],
            ),
        );


    $query = new WP_Query( $args );

    if( $query->have_posts() ) :
        while( $query->have_posts() ): $query->the_post();
            echo '<h5>' . $query->post->post_title . '</h5>';
        endwhile;
        wp_reset_postdata();
    else :
        echo 'No posts found';
    endif;

    die();
};

我们将一如既往地为您提供任何帮助!

【问题讨论】:

    标签: php wordpress custom-post-type custom-taxonomy


    【解决方案1】:

    这似乎得到了你所需要的:

    function misha_filter_function(){
        $taxonomies = get_taxonomies();;
        $args = array(
            'post_type' => 'cpt',
            'orderby' => 'date',
            'order'    => 'DESC',
        );
        $relation = 'AND';
        $params = array();
        $args['tax_query']['relation'] = $relation;
    
        foreach ( $taxonomies as $tax ) {
            if( isset( $_POST[ $tax . 'filter' ] ) && !empty( $_POST[ $tax . '_filter' ] ) {
                $args['tax_query'][] = array(
                        'taxonomy' => $tax,
                        'field' => 'id',
                        'terms' => $_POST[ $tax . 'filter' ],
                    );
            }
        }
    
        $query = new WP_Query( $args );
    
        if( $query->have_posts() ) :
            while( $query->have_posts() ): $query->the_post();
                echo '<h5>' . $query->post->post_title . '</h5>';
            endwhile;
            wp_reset_postdata();
        else :
            echo 'No posts found';
        endif;
    
        die();
    };
    

    【讨论】:

    • 感谢您抽出宝贵时间@Banzay!我想我应该注意到这段代码是抽象的。所以分类名称实际上是 event_category、incident_classification、incident_type 等。我将更改我的问题以包含该信息。
    • 实际上,我想这并不重要,但无论哪种方式,我仍然得到相同的结果。如果只发布了 1 个条件,则显示没有结果,但如果发布了所有条件,它仍然有效。
    • 好吧,我想通了。只需将if( isset( $_POST[ $tax . 'filter' ] ) ) 更改为if( isset( $_POST[ $tax . 'filter' ] ) &amp;&amp; !empty( $_POST[ $tax . '_filter' ] ) ) 如果您想编辑答案以包含该内容,我可以接受。
    • 对不起埃里克。我忘记了。最近,当我没有检查 var 的空值并得到错误的结果时,这是一个真正的问题。改变了答案。
    猜你喜欢
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 2014-09-30
    • 2016-01-14
    • 1970-01-01
    相关资源
    最近更新 更多