简体   繁体   中英

WP_Query custom post type by multiple custom fields

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:

Custom post type filter


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

}

You can pass an array of values to the value attribute of your meta_query . Further you can specify the relation the target meta values must have with your provided values.

Here is an example where you search for a post that has (besides the matching propert-price meta value) either value1 or value2 saved in the meta field 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'
        )
    )
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM