Wordpress-通过前端的多个自定义分类法筛选自定义帖子类型 [英] Wordpress - Filtering Custom Post Type by multiple Custom Taxonomies on front end

查看:47
本文介绍了Wordpress-通过前端的多个自定义分类法筛选自定义帖子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图允许用户从多个自定义分类法中选择条件,以便通过带有多个选择的from来过滤前端的自定义帖子类型.

I'm trying to allow users to select criteria from multiple custom taxonomies in order to filter a custom post type on the front end via a from with multiple selects.

我的问题是,只有在每个选择都选择了条件的情况下,该代码才有效.试图找到一种方法,使用户也可以只选择一个选项,并且只会按该选项过滤CPT.

My problem is that the code only works if each of the selects has criteria selected. Trying to find a way where users can also just select one of the options and it will only filter the CPT by that option.

*所有代码都是抽象的.分类名称实际上是:identification_category,incident_classification,incident_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();
};

对此的任何帮助将一如既往地受到赞赏!

Any help with this would be as always greatly appreciated!

推荐答案

这似乎可以满足您的需求:

This seems to get what you need:

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

这篇关于Wordpress-通过前端的多个自定义分类法筛选自定义帖子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆