【问题标题】:Wordpress AJAX Filter with CPT UI and multiple taxonmy带有 CPT UI 和多种分类的 Wordpress AJAX 过滤器
【发布时间】:2018-02-25 15:53:26
【问题描述】:

我为我的 WooCommerce 产品创建了一个 AJAX 过滤器。这在我使用工具集类型时有效,但现在在使用 CPT UI(自定义帖子类型 UI)创建自定义分类时不起作用。当我过滤时,它会起作用,但所有选项都不能一起使用。我什至创建了一个全新的安装来测试它。

所以,当我选择卡路里分类项目(它会在该选项中显示项目),然后选择膳食类型分类时,它会在膳食类型选项中显示项目,然后当我选择了过敏分类法,它只显示该膳食类型中的项目。

我希望它们协同工作,以便单击每个项目缩小范围并显示属于所有选定选项的项目。

functions.php

// WooCommerce AJAX Filter
function my_filters(){
$args = array(
    'post_type' => 'product',
    'posts_per_page' => -1,
    'orderby' => 'date',
    'order' => 'asc',
);

    if( isset( $_POST['caloriefilter'] ) )
    $args['tax_query'] = array(
      array(
          'taxonomy' => 'z_calories',
          'field' => 'id',
          'terms' => $_POST['caloriefilter']
      ),
    );

    if( isset( $_POST['mealtypefilter'] ) )
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'z_meal_type',
            'field' => 'id',
            'terms' => $_POST['mealtypefilter']
        ),
    );

    if( isset( $_POST['ingredientfilter'] ) )
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'z_ingredients',
            'field' => 'id',
            'operator' => 'NOT IN',
            'terms' => $_POST['ingredientfilter']
        ),
    );


$query = new WP_Query( $args );

if( $query->have_posts() ) :
    while( $query->have_posts() ): $query->the_post();
        wc_get_template_part( 'content', 'product' );
    endwhile;
    wp_reset_postdata();
else :
    echo 'No posts found';
endif;

die();
}

add_action('wp_ajax_customfilter', 'my_filters');
add_action('wp_ajax_nopriv_customfilter', 'my_filters');

page-title.php

<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">

                <h2>Calories</h2>
                <div class="divider div-transparent"></div>
                    <?php
                            if( $terms = get_terms( array(
                                'taxonomy' => 'z_calories',
                                'hide_empty' => false,
                                'orderby' => 'none',
                            )) ) :
                                    echo '<div class="row category-buttons">';
                                    foreach ( $terms as $term ) :
                                            echo '<div class="col-12 col-sm col-md"><input type="checkbox" id="meal-calorie-' . $term->term_id . '" value="' . $term->term_id . '" name="caloriefilter[]" /><label for="meal-calorie-' . $term->term_id . '">' . $term->name . '</label></div>';
                                    endforeach;
                                    echo '</div>';
                            endif;
                    ?>

                <h2>Meal Type</h2>
                <div class="divider div-transparent"></div>
                    <?php
                            if( $terms = get_terms( array(
                                'taxonomy' => 'z_meal_type',
                                'hide_empty' => false,
                                'orderby' => 'none',
                            )) ) :
                                    echo '<div class="row category-buttons">';
                                    foreach ( $terms as $term ) :
                                            echo '<div class="col-12 col-sm col-md"><input type="checkbox" id="meal-type-' . $term->term_id . '" value="' . $term->term_id . '" name="mealtypefilter[]" /><label for="meal-type-' . $term->term_id . '">' . $term->name . '</label></div>';
                                    endforeach;
                                    echo '</div>';
                            endif;
                    ?>

                    <a id="ingredientsToggle" class="clearfix" data-toggle="collapse" href="#ingredientsArea" aria-expanded="false" aria-controls="ingredientsArea"><button>Click for Allergies</button><h2>Allergies</h2></a>
                    <div class="collapse" id="ingredientsArea">
                        <div class="divider div-transparent"></div>
                        <p>Select items which you would like to avoid.</p>
                        <?php
                                if( $terms = get_terms( array(
                                    'taxonomy' => 'z_ingredients',
                                    'hide_empty' => false,
                                    'orderby' => 'name'
                                )) ) :
                                        echo '<ul class="ingredients-form">';
                                        foreach ( $terms as $term ) :
                                                echo '<li><input type="checkbox" name="ingredientfilter[]" id="ingredients-' . $term->term_id . '" value="' . $term->term_id . '" /><label for="ingredients-' . $term->term_id . '"><div><i class="fa fa-square-o fa-fw fa-2x"></i><i class="fa fa-check-square-o fa-fw fa-2x"></i></div>' . $term->name . '</label></li>';
                                        endforeach;
                                        echo '</ul>';
                                endif;
                        ?>
                    </div>
                    <input type="hidden" name="action" value="customfilter">
            </form><!-- END Filter Form -->

javascript.js

/* Custom Shop Filter */
jQuery(function($){
$('#filter input').on('change', function() {
    var filter = $('#filter');
    $.ajax({
        url:filter.attr('action'),
        data:filter.serialize(), // form data
        type:filter.attr('method'), // POST
        beforeSend:function(xhr){
            $('#loadingNotice').css('display' , 'block');          },
        success:function(data){
            $('#loadingNotice').css('display' , 'none');
            $('#response').html(data);
        }
    });
    return false;
});
});

【问题讨论】:

    标签: php ajax wordpress filter custom-taxonomy


    【解决方案1】:

    我找到了一个可行的解决方案。希望能找到更优雅的解决方案。

    functions.php

    // WooCommerce AJAX Filter
    function my_filters(){
    $args = array(
        'post_type' => 'product',
        'posts_per_page' => -1,
        'orderby' => 'date',
        'order' => 'asc',
    );
    
        if( isset( $_POST['caloriefilter'] ) )
        $args['tax_query'] = array(
          array(
              'taxonomy' => 'z_calories',
              'field' => 'id',
              'terms' => $_POST['caloriefilter']
          ),
        );
    
        if( isset( $_POST['mealtypefilter'] ) )
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'z_meal_type',
                'field' => 'id',
                'terms' => $_POST['mealtypefilter']
            ),
        );
    
        if( isset( $_POST['ingredientfilter'] ) )
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'z_ingredients',
                'field' => 'id',
                'operator' => 'NOT IN',
                'terms' => $_POST['ingredientfilter']
            ),
        );
    
        if( isset( $_POST['caloriefilter'] ) && isset( $_POST['mealtypefilter'] ) )
        $args['tax_query'] = array(
          array(
              'taxonomy' => 'z_calories',
              'field' => 'id',
              'terms' => $_POST['caloriefilter']
          ),
          array(
              'taxonomy' => 'z_meal_type',
              'field' => 'id',
              'terms' => $_POST['mealtypefilter']
          ),
        );
    
        if( isset( $_POST['caloriefilter'] ) &&  isset( $_POST['ingredientfilter'] ) )
        $args['tax_query'] = array(
          array(
              'taxonomy' => 'z_calories',
              'field' => 'id',
              'terms' => $_POST['caloriefilter']
          ),
    
          array(
              'taxonomy' => 'z_ingredients',
              'field' => 'id',
              'operator' => 'NOT IN',
              'terms' => $_POST['ingredientfilter']
          ),
        );
    
        if( isset( $_POST['mealtypefilter'] ) && isset( $_POST['ingredientfilter'] ) )
        $args['tax_query'] = array(
          array(
              'taxonomy' => 'z_meal_type',
              'field' => 'id',
              'terms' => $_POST['mealtypefilter']
          ),
          array(
              'taxonomy' => 'z_ingredients',
              'field' => 'id',
              'operator' => 'NOT IN',
              'terms' => $_POST['ingredientfilter']
          ),
        );
    
        if( isset( $_POST['caloriefilter'] ) && isset( $_POST['mealtypefilter'] ) && isset( $_POST['ingredientfilter'] ) )
        $args['tax_query'] = array(
          array(
              'taxonomy' => 'z_calories',
              'field' => 'id',
              'terms' => $_POST['caloriefilter']
          ),
          array(
              'taxonomy' => 'z_meal_type',
              'field' => 'id',
              'terms' => $_POST['mealtypefilter']
          ),
          array(
              'taxonomy' => 'z_ingredients',
              'field' => 'id',
              'operator' => 'NOT IN',
              'terms' => $_POST['ingredientfilter']
          ),
        );
    
    
    
    
    
    
    $query = new WP_Query( $args );
    
    if( $query->have_posts() ) :
        while( $query->have_posts() ): $query->the_post();
            wc_get_template_part( 'content', 'product' );
        endwhile;
        wp_reset_postdata();
    else :
        echo 'No posts found';
    endif;
    
    die();
    }
    
    
    add_action('wp_ajax_customfilter', 'my_filters');
    add_action('wp_ajax_nopriv_customfilter', 'my_filters');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-18
      • 2015-04-17
      • 1970-01-01
      • 1970-01-01
      • 2020-03-05
      • 2021-03-28
      • 1970-01-01
      相关资源
      最近更新 更多