Loop order issue with Ajax filter
Question
I’m building a filter with AJAX to sort a custom post type loop. Users can filter the loop using two taxonomies “group” and “area”.
Some of the args of my CPT loop are:
'orderby' => 'date',
'order' => 'ASC'
The issue is, the filter works great but the result is displayed in the DESC order not ASC.
Here is the code of the filter:
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( $areas = get_terms( array( 'taxonomy' => 'area' ) ) ) :
$areas_terms = array();
foreach( $areas as $area ) {
if( isset( $_POST['area_' . $area->term_id ] ) && $_POST['area_' . $area->term_id] == 'on' )
$areas_terms[] = $area->slug;
}
endif;
$tax_query = array( 'relation' => 'AND' );
if ( ! empty( $groups_terms ) ) {
$tax_query[] = array(
'taxonomy' => 'group',
'field' => 'slug',
'terms' => $groups_terms,
);
}
if ( ! empty( $areas_terms ) ) {
$tax_query[] = array(
'taxonomy' => 'area',
'field' => 'slug',
'terms' => $areas_terms,
);
}
$args = array(
'post_type' => 'workshop',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => -1,
'tax_query' => $tax_query
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
get_template_part( 'template-parts/content-archive-workshop' );
endwhile;
else :
?>
<section class="section bg-grey section-split--2 height-elastic">
<div class="container">
<div class="container-content col-1-2 padding--small">
<h2 class="entry-title large"><?php esc_html_e( 'No se han encontrado talleres', 'artonyou' ); ?></h2>
</div>
<div class="container-content col-1-2 "></div>
</div>
</section>
<?php
endif;
die();
}
add_action('wp_ajax_myfilter', 'mysite_filter_function');
add_action('wp_ajax_nopriv_myfilter', 'mysite_filter_function');
Any help would be much appreciated, thank you.
0
1 year
2021-11-10T08:27:20-05:00
2021-11-10T08:27:20-05:00 0 Answers
0 views
0
Leave an answer