php – Filtruj posty za pomocą formularza ajax i pól wyboru
Mam pętlę dla mojego niestandardowego typu postów workshop
filtrowane za pomocą dwóch niestandardowych taksonomii group
I teacher
na tej samej stronie. Filtr Ajax działa idealnie.
Każde łącze filtra działa z <input type="checkbox">
, dla obu list taksonomii. Gdy użytkownik zaznaczy w filtrze jedno lub wiele terminów, a następnie odznaczy je wszystkie, filtr zwróci komunikat „Nie znaleziono wpisów”, ponieważ nic nie jest zaznaczone.
To, co chciałbym zrobić, to gdy użytkownik odznaczy wszystkie warunki w filtrze, wyświetli wszystkie posty mojego niestandardowego typu postów.
Kod PHP filtra dla frontendu:
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
<div>
<?php
if( $groups = get_terms( array( 'taxonomy' => 'group' ) ) ) :
echo '<ul class="groups-list">';
foreach( $groups as $group ) :
echo '<input type="checkbox" class="" id="group_' . $group->term_id . '" name="group_' . $group->term_id . '" /><label for="group_' . $group->term_id . '">' . $group->name . '</label>';
endforeach;
echo '</ul>';
endif;
?>
</div>
<div>
<?php
if( $teachers = get_terms( array( 'taxonomy' => 'teacher' ) ) ) :
echo '<ul class="teachers-list">';
foreach( $teachers as $teacher ) :
echo '<input type="checkbox" class="" id="teacher_' . $teacher->term_id . '" name="teacher_' . $teacher->term_id . '" /><label for="teacher_' . $teacher->term_id . '">' . $teacher->name . '</label>';
endforeach;
echo '</ul>';
endif;
?>
</div>
<input type="hidden" name="action" value="myfilter">
</form>
Kod PHP dla filtra AJAX w functions.php:
function mysite_filter_function(){
//groups checkboxes
if( $groups = get_terms( array( 'taxonomy' => 'group' ) ) ) :
$groups_terms = array();
foreach( $groups as $group ) {
if( isset( $_POST['group_' . $group->term_id ] ) && $_POST['group_' . $group->term_id] == 'on' )
$groups_terms[] = $group->slug;
}
endif;
//teachers checkboxes
if( $teachers = get_terms( array( 'taxonomy' => 'teacher' ) ) ) :
$teachers_terms = array();
foreach( $teachers as $teacher ) {
if( isset( $_POST['teacher_' . $teacher->term_id ] ) && $_POST['teacher_' . $teacher->term_id] == 'on' )
$teachers_terms[] = $teacher->slug;
}
endif;
if (empty($groups_terms) || empty($teachers_terms) ) {
$relation = 'OR';
}else{
$relation = 'AND';
}
$args = array(
'orderby' => 'date',
'post_type' => 'workshop',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => $relation,
array(
'taxonomy' => 'group',
'field' => 'slug',
'terms' => $groups_terms
),
array(
'taxonomy' => 'teacher',
'field' => 'slug',
'terms' => $teachers_terms
)
)
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
echo '<h2>' . $query->post->post_title . '</h2>';
endwhile;
wp_reset_postdata();
else :
echo 'No posts found';
endif;
die();
}
add_action('wp_ajax_myfilter', 'mysite_filter_function');
add_action('wp_ajax_nopriv_myfilter', 'mysite_filter_function');
Kod JS:
$('#filter').change(function(){
var filter = $('#filter');
$.ajax({
url:filter.attr('action'),
data:filter.serialize(),
type:filter.attr('method'),
beforeSend:function(xhr){
//filter.find('button').text('Processing...');
},
success:function(data){
//filter.find('button').text('Filter');
$('.loop-archive-workshop').html(data);
}
});
return false;
});
Dzięki!