Я понял это. Вот финальный скрипт для 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;
});
});