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

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

问题描述:

好吧,伙计们,我在我头上。WP_Query自定义帖子类型由多个自定义字段

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

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

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

我试图做到的是让一个查询往那个过滤所有的别墅“使用表单值。广泛的谷歌搜索后,我发现有可能通过使用meta_key的自定义字段循环自定义post_type的。 Basicly什么,我试图做的是:

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

} 

您可以通过值数组meta_queryvalue属性。此外,您可以指定目标元值与您提供的值之间的关系。

这里是保存在元字段,您搜索具有后(除了匹配propert-price元值)的一个例子要么value1value2property-region

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