wordpress 从 AJAX 筛选中排除类别

4nkexdtk  于 2022-11-22  发布在  WordPress
关注(0)|答案(1)|浏览(159)

我有类别过滤器显示自定义帖子类型的类别。如何从过滤中排除具有ID的特定类别?
我用了 'category__not_in' =〉12,但似乎它不工作的过滤器,只有隐藏的职位是什么类别12。
AJAX 过滤器:

function filter_ajax() {
    
    $category = $_POST['category'];
    
    $args = array(
            'post_type' => 'pozice',
            'category__not_in' => 12,
            'posts_per_page' => -1
            );
    
    if(isset($category)) {
        $args['category__in'] = array($category);
    }
    
    $query = new WP_Query($args);
    
    if($query->have_posts()) :
        while($query->have_posts()) : 
            $query->the_post();
            include("content_pozice_box.php");
        endwhile;
    endif;
    wp_reset_postdata(); 
    
    die();
}

JS:

(function($){

    $(document).ready(function(){
        $(document).on('click', '.js-filter-item > a', function(e){
            e.preventDefault();

            var category = $(this).data('category');

            $.ajax({
                url:wp_ajax.ajax_url,
                data: { action: 'filter', category: category },
                type: 'post',
                success: function(result) {
                    $('.js-filter').html(result);
                },
                error: function(result) {
                    console.warn(result);
                }
            });
        });
    });

})(jQuery);

用于在前端显示带过滤器的帖子的功能:

function make_filters_shortcode($atts) {
?>
    <div class="categories">
        <ul>
            <li class="js-filter-item"><a href="<?= home_url(); ?>">Všechny pozice</a></li>
<?php 
    $cat_args = array(
        'exclude' => array(1),
        'category__not_in' => 12,
        'option_all' => 'Všechny pozice'
    );
    
    $categories = get_categories($cat_args);
    
    foreach($categories as $cat) : 
?>
        <li class="js-filter-item"><a data-category="<?= $cat->term_id;?>" href="<?= get_category_link($cat->term_id); ?>"><?= $cat->name; ?></a></li>
<?php 
    endforeach; 
?>
    </ul>
    </div>
    
    <div class="js-filter hp">
<?php
    
    $args = array(
        'post_type' => 'pozice',
        'category__not_in' => 12,
        'posts_per_page' => 4
        );

    $query = new WP_Query($args);

    if($query->have_posts()) :
        while($query->have_posts()) : 
            $query->the_post();
            include("content_pozice_box.php");
        endwhile;
    endif;
    wp_reset_postdata(); 
?>
    </div>
<?php
    }
    add_shortcode('job-filters', 'make_filters_shortcode');
atmip9wb

atmip9wb1#

你能不能把这个category__not_in' =〉12替换成'category__not_in' =〉数组(12)。
示例代码:

<?php
  $args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'category__not_in' => array( 55 )
  );
  $query = new WP_Query( $args );
?>

<?php
  if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
      $query->the_post();
?>

// code

<?php
    }
  } else {
    // no posts found
  }
  wp_reset_postdata();
?>

相关问题