Ajax过滤器帖子按类别

时间:2019-10-15 12:08:26

标签: jquery wordpress checkbox

我有2个父类别(;With DeleteCTE as ( select [ID], Code from Discipline where Code like '%DHA-DIS%' ) Delete from Discipline D Inner Join DeleteCTE DC on DC.ID = D.ID country),并得到它们的子类别。现在我要过滤它们。

我关注了这篇文章:https://rudrastyh.com/wordpress/ajax-post-filters.html

enter image description here

但是,当我单击fruit时,我只想显示Apple的苹果,而不显示Netherlands或任何其他国家的苹果。

这是我的Spain

checkboxes

这是我的过滤器功能:

<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 />';
            }
        ?>

这是过滤器脚本:

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(
    'orderby' => 'date', // we will sort posts by date
    'order' => $_POST['date'], // ASC or DESC
    'posts_per_page' => -1 // show all posts.
);

// category filter function
if( isset( $_POST['categoryfilter_country'] ) && isset 
($_POST['categoryfilter_fruit']) )
    $args['tax_query'] = array( 'relation' => 'AND' );
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'category',
            'field' => 'id',
            'terms' => $_POST['categoryfilter_country']
        ),
        array(
            'taxonomy' => 'category',
            'field' => 'id',
            'terms' => $_POST['categoryfilter_fruit']
        )
    );

$query = new WP_Query( $args );

// query here ...

die();
}

1 个答案:

答案 0 :(得分:0)

我知道了。这是2组复选框的最终脚本(“ Vertragsart” +“ Zeitmodell”)。当您选中一个组中的一个复选框(例如“ Zeitmodell”)时,它会过滤帖子-当您另外选中另一个组中的一个复选框(例如“ 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脚本(未更改):

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