好吧,夥計們,我在我頭上。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......
)
);
}
但是,我不能爲我的生活弄清楚如何通過與可變元值的職位篩選,從形式發送。
如果任何人都可以指出我正確的方向,它將非常感激。
爲了給你一個想法,這是過濾器是什麼樣子:
編輯
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();
}