WP_Query自定义帖子类型由多个自定义字段组成

时间:2015-11-05 12:30:18

标签: php wordpress custom-post-type custom-fields wp-query

好吧,伙计们,我已经超过了我的头脑。

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

的组中。

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

我发送的数据包括:
- 区域,类型,样式选择字段;
- 海景,海上通道,游泳池,改革项目复选框;
- 价格输入字段

我想要完成的是获取一个过滤所有' Villas'使用表单值。经过大量的Google搜索后,我发现可以使用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......
            )
        );
    }

但是,我不能为我的生活弄清楚如何通过具有可变元值的帖子进行过滤,从表单发送。

如果有人能指出我正确的方向,那将非常感激。

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

Custom post type filter

编辑
在xphan的建议之后我编辑了我的代码,但是即使_GET的填充正确,var_dump也不会返回任何内容。

<?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();

}

1 个答案:

答案 0 :(得分:1)

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

以下是一个示例,您可以在其中搜索具有({1}}元数值除外){$ 1}}或meta_query保存在元字段propert-price <中的帖子/ p>

value1
相关问题