Ajax Post Filter Tutorial with one taxonomy : How to get multiple taxonomies?

13 views
0

I’m new on this website, I already used a lot of info on Magento stackexchange by the past, but now I’m working on worpdress πŸ™‚

Pretty easier than Magento, but still hards sometimes, maybe because I’m not an official developper πŸ˜‰

Well, I found a brilliant tutorial to implement AJAX filtering on taxonomies for custom post.

In the tutorial, there is only one taxonomy.

I succeeded modifying the code to filter two taxonomies BUT for just only one taxonomy and the empty other, there is no results and I didn’t know how to construct my php code / query.

Could someone give me a little help ? Thanks a lot.

I’m gonna post all the differents code :
(Just the function part, because it’s all I need for the moment, construction of the query)

  • Author code
  • The author code modified by myself
  • The suggestion by the author for a two filters taxonomies

Many Many thanks for your help.

ORIGINAL CODE

function misha_filter_function(){
$args = array(
    'orderby' => 'date', // we will sort posts by date
    'order' => $_POST['date'] // ASC ??? DESC
);

// for taxonomies / categories
if( isset( $_POST['categoryfilter'] ) )
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'category',
            'field' => 'id',
            'terms' => $_POST['categoryfilter']
        )
    );

// create $args['meta_query'] array if one of the following fields is filled
if( isset( $_POST['price_min'] ) && $_POST['price_min'] || isset( $_POST['price_max'] ) && $_POST['price_max'] || isset( $_POST['featured_image'] ) && $_POST['featured_image'] == 'on' )
    $args['meta_query'] = array( 'relation'=>'AND' ); // AND means that all conditions of meta_query should be true

// if both minimum price and maximum price are specified we will use BETWEEN comparison
if( isset( $_POST['price_min'] ) && $_POST['price_min'] && isset( $_POST['price_max'] ) && $_POST['price_max'] ) {
    $args['meta_query'][] = array(
        'key' => '_price',
        'value' => array( $_POST['price_min'], $_POST['price_max'] ),
        'type' => 'numeric',
        'compare' => 'between'
    );
} else {
    // if only min price is set
    if( isset( $_POST['price_min'] ) && $_POST['price_min'] )
        $args['meta_query'][] = array(
            'key' => '_price',
            'value' => $_POST['price_min'],
            'type' => 'numeric',
            'compare' => '>'
        );

    // if only max price is set
    if( isset( $_POST['price_max'] ) && $_POST['price_max'] )
        $args['meta_query'][] = array(
            'key' => '_price',
            'value' => $_POST['price_max'],
            'type' => 'numeric',
            'compare' => '<'
        );
}


// if post thumbnail is set
if( isset( $_POST['featured_image'] ) && $_POST['featured_image'] == 'on' )
    $args['meta_query'][] = array(
        'key' => '_thumbnail_id',
        'compare' => 'EXISTS'
    );

$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', 'misha_filter_function'); 
add_action('wp_ajax_nopriv_myfilter', 'misha_filter_function');

CODE MODIFIED
When the two taxonomies is filtered, it works well, but if just only one, there is no results.

// for taxonomies / if destinations and activities are set
if( isset( $_POST['categoryfilter'] ) && isset ($_POST['activitefilter'])  ) {
    $args['tax_query'] = array(

    'relation' => 'AND',

        array(
            'taxonomy' => 'destination',
            'field' => 'id',
            'terms' => $_POST['categoryfilter']
        ),
        array(
            'taxonomy' => 'activite',
            'field' => 'id',
            'terms' => $_POST['activitefilter']
        ),
    );


} else {
if( isset( $_POST['categoryfilter']) || !empty( $_POST['activitefilter'] ) )// for taxonomies / only destinations are set
    $args['tax_query'] = array(
            'taxonomy' => 'destination',
            'field' => 'id',
            'terms' => $_POST['categoryfilter']
    );
if( !empty( $_POST['categoryfilter']) || isset( $_POST['activitefilter'] ) )// for taxonomies / only activities are set
    $args['tax_query'] = 
        array(
            'taxonomy' => 'activite',
            'field' => 'id',
            'terms' => $_POST['activitefilter']
    );

}

A PIECE OF SOlUTION BY THE AUTHOR

// first of all before filtering define the empty tax_query
$args['tax_query'] = array();

// or with relation
$args['tax_query'] = array( 'relation' => 'AND' );

// after that when you begin filtering it, add elements to array with
// $args['tax_query'][] = 'NEW ELEMENT';
// example: 
$args['tax_query'][] = array(
'taxonomy' => 'activite',
'field' => 'id',
'terms' => $_POST['activitefilter']
);