Jquery Ajax按类别筛选帖子

Jquery Ajax按类别筛选帖子,jquery,wordpress,checkbox,Jquery,Wordpress,Checkbox,我有两个父类别(国家,水果),并得到它们的子类别。现在我想过滤它们 我跟随这篇文章: 但是当我点击苹果时,我只想显示荷兰的苹果,而不是西班牙或任何其他国家的苹果 这是我的复选框: <h3>Country</h3> <?php $args = array('child_of' => 89); $categories = get_categories( $args ); fo

我有两个父类别(
国家
水果
),并得到它们的子类别。现在我想过滤它们

我跟随这篇文章:

但是当我点击苹果时,我只想显示荷兰的苹果,而不是西班牙或任何其他国家的苹果

这是我的
复选框

<h3>Country</h3>
        <?php
            $args = array('child_of' => 89);
            $categories = get_categories( $args );
            foreach($categories as $category) { 
                echo '<input type="checkbox" name="categoryfilter[]" 
value="'.$category->cat_ID.'"> '.$category->name.'<br />';
            }
        ?>

        <h3>Fruit</h3>
        <?php
            $args = array('child_of' => 84);
            $categories = get_categories( $args );
            foreach($categories as $category) { 
                echo '<input type="checkbox" name="categoryfilter[]" 
value="'.$category->cat_ID.'"> '.$category->name.'<br />';
            }
        ?>
这是筛选脚本:

jQuery(function($){
            $('#filter').change(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
                        $('#response').html(data); // insert data
                    }
                });
                return false;
            });
        });

我已经弄明白了。下面是两组复选框(“Vertragsart”+“Zeitmodell”)的最终脚本。当您勾选一个组中的复选框(如“ZeitModel”)时,它会过滤帖子-当您另外勾选另一个组中的复选框(如“Vertragsart”)时,脚本会检查两者是否匹配(即“帖子共享同一类别”)

功能:

add_action('wp_ajax_myfilter', 'misha_filter_function');
add_action('wp_ajax_nopriv_myfilter', 'misha_filter_function');

function misha_filter_function(){
$args = array(
    'orderby' => 'date',
    'order' => $_POST['date'],
    'posts_per_page' => -1
);

// for taxonomies / categories
if( isset( $_POST['vertragsart'] ) && isset ($_POST['zeitmodell'])  ) {
    $args['tax_query'][] = array(
    // 'relation' => 'AND',

        array(
            'taxonomy' => 'category',
            'field' => 'id',
            'terms' => $_POST['vertragsart']
        ),
        array(
            'taxonomy' => 'category',
            'field' => 'id',
            'terms' => $_POST['zeitmodell']
        ),
    );

} elseif( !isset( $_POST['vertragsart'] ) && isset ($_POST['zeitmodell'])  ) {
    $args['tax_query'][] = array(
        'taxonomy' => 'category',
        'field' => 'id',
        'terms' => $_POST['zeitmodell']
    );

} elseif( isset( $_POST['vertragsart'] ) && !isset ($_POST['zeitmodell'])  ) {
    $args['tax_query'][] = array(
        'taxonomy' => 'category',
        'field' => 'id',
        'terms' => $_POST['vertragsart']
    );
}

$query = new WP_Query( $args );

if( $query->have_posts() ) :
    while( $query->have_posts() ): $query->the_post();
        echo '<article>';
        echo '<h4>' . $query->post->post_title . '</h4>';
        get_template_part('/template_parts/categories_jobs');
        echo '</article>';
    endwhile;
    wp_reset_postdata();
else :
    echo 'Im Moment keine Stellenangebote.';
endif;
die();
}
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">

        <h3>Vetragsart</h3>
        <?php
            $args = array('child_of' => 92);
            $categories = get_categories( $args );
            foreach($categories as $category) { 
                echo '<input type="checkbox" name="vertragsart[]" value="'.$category->cat_ID.'"> '.$category->name.'<br />';
            }
        ?>

        <br />
        <h3>Zeitmodell</h3>
        <?php
            $args = array('child_of' => 93);
            $categories = get_categories( $args );
            foreach($categories as $category) { 
                echo '<input type="checkbox" name="zeitmodell[]" value="'.$category->cat_ID.'"> '.$category->name.'<br />';
            }
        ?>

        <!-- <button>Apply filter</button> -->
        <input type="hidden" name="action" value="myfilter">
    </form>
jQuery(function($){
            $('#filter').change(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
                        $('#response').html(data); // insert data
                    }
                });
                return false;
            });
        });
jQuery(function($){
            $('#filter').change(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
                        $('#response').html(data); // insert data
                    }
                });
                return false;
            });
        });