从ajax过滤中排除类别



我有分类过滤器显示自定义帖子类型的类别。如何从过滤中排除具有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');

你能不能把这个category替换掉__not_in' =>12 with '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();
?>

最新更新