Home > Blockchain >  Custom post type filter by taxonomies
Custom post type filter by taxonomies

Time:01-16

I'm trying to filter posts by their taxonomy.

I've a custom post type called Properties.

I have two blocks one with the taxonomies "house" and "Europe" selected and another one with the taxonomies "house" and "Africa" selected I'm using the following code:

        $args = array(
            'post_type'         => 'property',
            'posts_per_page'    => -1,
            'post_status'       => 'publish',
            'tax_query' => array(
                array(
                    'taxonomy' => 'location',
                    'field'    => 'slug',
                    'terms'    =>  array('europe','house'),
                    'operator' => 'IN'
                ),
            ),
            'meta_query'    => array(
                'relation'      => 'AND',
                array(
                    'key'       => 'guests',
                    'value'     => $guests,
                    'compare'   => 'LIKE'
                ),
                array(
                    'key'       => 'beds',
                    'value'     => $beds,
                    'compare'   => 'LIKE'
                ),
                array(
                    'key'       => 'baths',
                    'value'     => $baths,
                    'compare'   => 'LIKE'
                )
            )
        );

If I filter by Europe and House it's displaying both blocks because both have the taxonomy house. I'd like to show only the one with "Europe" and "house" ones.

Any ideas?

I've tried using other operators but none of them are working as I'd like.

CodePudding user response:

You can try this :

$args = array(
    'post_type'         => 'property',
    'posts_per_page'    => -1,
    'post_status'       => 'publish',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'location',
            'field'    => 'slug',
            'terms'    =>  array('europe'),
        ),
        array(
            'taxonomy' => 'location',
            'field'    => 'slug',
            'terms'    =>  array('house'),
        ),
    ),
    'meta_query'    => array(
        'relation'      => 'AND',
        array(
            'key'       => 'guests',
            'value'     => $guests,
            'compare'   => 'LIKE'
        ),
        array(
            'key'       => 'beds',
            'value'     => $beds,
            'compare'   => 'LIKE'
        ),
        array(
            'key'       => 'baths',
            'value'     => $baths,
            'compare'   => 'LIKE'
        )
    )
);

CodePudding user response:

Solved! I've done it in the following way:

$taxonomies = $_REQUEST['taxonomies'];
//Get locations & remove from general taxs
    $locations = get_term_children(58,'location');
    $locationsToFilter = array();

    //Get Property type & remove from general taxs
    $propertyType = get_term_children(59,'location');
    $propertyTypeToFilter = array();

    if($taxonomies){
        foreach($taxonomies as $current_tax){
            if(in_array($current_tax, $locations)){
                array_push($locationsToFilter, $current_tax);
                if (($key = array_search($current_tax, $taxonomies)) !== false) {
                    unset($taxonomies[$key]);
                } 

            }
            if(in_array($current_tax, $propertyType)){
                array_push($propertyTypeToFilter, $current_tax);
                if (($key = array_search($current_tax, $taxonomies)) !== false) {
                    unset($taxonomies[$key]);
                } 
            }
        }
    }

    $the_query = new WP_Query( $args );
    $numberOfPosts = 0;
    while ( $the_query->have_posts() ) : $the_query->the_post(); $property = get_the_ID();
    $propertyTerms = wp_get_post_terms($property, 'location', array('fields' => 'ids'));

    //Check if property is in requested locations
    if($locationsToFilter){
        if(!array_intersect($propertyTerms, $locationsToFilter)){
            //This property is not included within any of the requested locations
            continue;
        }
    }
    //Check if property type is in requested locations
    if($propertyTypeToFilter){
        if(!array_intersect($propertyTerms, $propertyTypeToFilter)){
            //This property is not included within any of the requested property type
        continue;
        }
    }
    //Check remaining taxonomies and filter out any properties that don't contain all of them
    if($taxonomies){
        foreach($taxonomies as $tax_check){
            if(!in_array($tax_check, $propertyTerms)){
                continue 2;
            }
        }
    }

    $numberOfPosts  ;
  • Related