Alright guys, I'm in way over my head.
I'm trying to build a filter for a custom post type, 'Villas'. These villas have multiple custom fields, these custom fields exist in a group named 'features'.
I've build a search / filter form that POST's the data, which I'm then able to capture using a $_GET request.
The data I'm sending includes:
- Region, type, style select fields;
- Sea view, Sea access, Swimming pool, Reform project checkboxes;
- Price input field
What I'm trying to accomplish is get a query going that filters all 'Villas' using the form values. After extensive googling I found it is possible to loop through custom post_type's with custom fields by using meta_key's. Basicly what I'm trying to do is:
$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......
)
);
}
However, I cannot for the life of me figure out how to filter through the posts with variable meta values, send from the form.
If anyone could point me in the right direction, it would be extremely appreciated.
To give you an idea, this is what the filter looks like:
EDIT
After xphan's suggestion I edited my code like so, however the var_dump returns nothing even though the _GET's are filled correctly.
<?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();
}