Filter posts with ajax form and checkboxes

What I would like to do is, when the user unselects all the terms in the filter it displays all the posts of my Custom Posts Type.

In that case, then make the tax query optional:

// In mysite_filter_function(), define $args like so:

$tax_query = array( 'relation' => 'AND' );

if ( ! empty( $groups_terms ) ) {
    $tax_query[] = array(
        'taxonomy' => 'group',
        'field'    => 'slug',
        'terms'    => $groups_terms,
    );
}

if ( ! empty( $teachers_terms ) ) {
    $tax_query[] = array(
        'taxonomy' => 'teacher',
        'field'    => 'slug',
        'terms'    => $teachers_terms,
    );
}

$args = array(
    'orderby'        => 'date',
    'post_type'      => 'workshop',
    'posts_per_page' => -1,
    'tax_query'      => $tax_query,
);

Leave a Comment