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

kkih6yb8  于 2023-01-16  发布在  PHP
关注(0)|答案(1)|浏览(128)

我正试着按分类法过滤帖子。
我有一个自定义的文章类型称为属性。
我有两个块,一个选择了分类"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'
                )
            )
        );

如果我按Europe和House过滤,它会显示两个区块,因为它们都有分类house。我只想显示有"Europe"和"house"的区块。
有什么想法吗?
我试过使用其他操作符,但没有一个能像我希望的那样工作。

xdnvmnnf

xdnvmnnf1#

你可以试试这个:

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

相关问题