WP_Query 自定义 post 按多个自定义字段输入

WP_Query custom post type by multiple custom fields

好的伙计们,我有点头疼了。

我正在尝试为自定义 post 类型 'Villas' 构建过滤器。这些别墅有多个自定义字段,这些自定义字段存在于一个名为'features'.

的组中

我构建了一个搜索/过滤表单,其中 POST 是数据,然后我可以使用 $_GET 请求捕获这些数据。

我发送的数据包括:
- 地区、类型、风格 select 字段;
- 海景、出海通道、游泳池、改革项目复选框;
- 价格输入字段

我想要完成的是获取一个使用表单值过滤所有 'Villas' 的查询。经过广泛的谷歌搜索后,我发现可以通过使用 meta_key 循环遍历带有自定义字段的自定义 post_type。基本上我想做的是:

    $propertyPrice      = $_GET['price'];
    $propertyRegion     = $_GET['region'];
    $propertyType       = $_GET['type'];
    $propertyStyle      = $_GET['style'];
    $hasSeaview         = $_GET['seaview'];
    $hasSeaAccess       = $_GET['sea-access'];
    $hasSwimmingPool    = $_GET['swimming-pool'];
    $hasReformProject   = $_GET['reform-project'];

    if( isset($propertyPrice) || isset($propertyRegion || isset($propertyType)) || isset($propertyStyle) || isset($hasSeaview) || isset($hasSeaAccess) || isset($hasSwimmingPool) || isset($hasReformProject)) {
        $args = array(
            'meta_query' => array(
                'relation' => 'OR'
                array(
                    'key'   => 'property-price', 
                    'value' => $propertyPrice,
                ),
                array(
                    'key'   => 'property-region', 
                    'value' => $propertyRegion,
                ),
                array(
                    'key'   => 'property-type', 
                    'value' => $propertyType,
                ),
                etc......
            )
        );
    }

但是,我终生无法弄清楚如何过滤具有可变元值的 posts,从表单发送。

如果有人能指出我正确的方向,将不胜感激。


给你一个想法,这是过滤器的样子:

Custom post type filter


编辑
在 xphan 的建议之后,我像这样编辑了我的代码,但是 var_dump returns 什么也没有,即使 _GET 被正确填充。

<?php
    $propertyPrice      = $_GET['price'];   
    $propertyRegion     = $_GET['region'];
    if($propertyRegion === 'all') { $propertyRegion = array('ibiza-city', 'southwest', 'north', 'east', 'center'); }
    $propertyType       = $_GET['type'];
    if($propertyType === 'all') { $propertyType = array('villa', 'apartment', 'plot'); }
    $propertyStyle      = $_GET['style'];
    if($propertyStyle === 'all') { $propertyStyle = array('rustic', 'modern'); }
    $hasSeaview         = $_GET['seaview'];
    if( isset($hasSeaview) ) { $hasSeaview = 1; }
    $hasSeaAccess       = $_GET['sea-access'];
    if( isset($hasSeaAccess) ) { $hasSeaAccess = 1; }
    $hasSwimmingPool    = $_GET['swimming-pool'];
    if( isset($hasSwimmingPool) ) { $hasSwimmingPool = 1; }
    $hasReformProject   = $_GET['reform-project'];
    if( isset($hasReformProject) ) { $hasReformProject = 1; }
?>

<?php

echo $propertyRegion .'<br>';
echo $propertyType .'<br>';
echo $propertyStyle .'<br>';
echo $propertyPrice .'<br>';
?>
<?php  if( isset($propertyPrice) || isset($propertyRegion) || isset($propertyType) || isset($propertyStyle) || isset($hasSeaview) || isset($hasSeaAccess) || isset($hasSwimmingPool) || isset($hasReformProject)) { 
    $args = array(
        'post_type' => 'villas',
        'meta_query' => array(
            array(
                'key'   => 'property-price', 
                'value' => $propertyPrice
            ),
            array(
                'key'   => 'property-region', 
                'value' => $propertyRegion,
                'compare' => 'IN'
            ),
            array(
                'key'   => 'property-type', 
                'value' => $propertyType,
                'compare' => 'IN'
            ),
            array(
                'key'   => 'property-style', 
                'value' => $propertyStyle,
                'compare' => 'IN'
            ),
            array(
                'key'   => 'sea-view', 
                'value' => $hasSeaview
            )
        ) 
    ); 

    $the_query = new WP_Query( $args );

    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
            $the_query->the_post(); ?>

            <?php var_dump($the_query->the_post()); ?>

<?php
        }
    } else {
        // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();

}

您可以将一组值传递给meta_queryvalue属性。此外,您可以指定目标元值必须与您提供的值之间的关系。

这是一个示例,您在其中搜索 post,其中(除了匹配的 propert-price 元值之外)元字段中保存了 value1value2 property-region

$args = array(
    'meta_query' => array(
        'relation' => 'OR'
        array(
            'key'   => 'property-price', 
            'value' => $propertyPrice,
        ),
       array(
            'key' => 'property-region',
            'value' => array('value1', 'value2'),
            'compare' => 'IN'
        )
    )
);