简体   繁体   English

按分类法自定义帖子类型过滤器

[英]Custom post type filter by taxonomies

I'm trying to filter posts by their taxonomy.我正在尝试按分类过滤帖子。

I've a custom post type called Properties.我有一个名为 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:我有两个块,一个选择了分类法“house”和“Europe”,另一个选择了分类法“house”和“Africa”我正在使用以下代码:

        $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.如果我按 Europe 和 House 过滤,它会显示这两个街区,因为它们都有分类学房子。 I'd like to show only the one with "Europe" and "house" ones.我只想展示带有“Europe”和“house”的那个。

Any ideas?有任何想法吗?

I've tried using other operators but none of them are working as I'd like.我试过使用其他操作员,但没有一个像我想的那样工作。

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'
        )
    )
);

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++;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM