Advanced WP_Query Issue
-
[ redundant link removed ]
In the above url when I select the 2 Bedrooms option from Bedrooms dropdown, a Parking property is also shown in the results.This is probably because the bedrooms field is filled as 2 from the admin panel for that particular Parking.
Note: parking is in taxonomy building-typepublic function getPropertiesPosts() { $Request = $this->filters; $postsPerPage = get_field('number_of_posts_per_page'); $conditions = []; $order = []; $taxConditions = []; $queryParams = []; // Set propertyType Filter if (isset($Request['buildingType']) && $Request['buildingType'] != '') { $buildingTypes = explode('-and-', $Request['buildingType']); $taxConditions[] = array( 'taxonomy' => 'building-type', 'field' => 'slug', 'terms' => $buildingTypes, ); } // Set bedrooms Filter if (isset($Request['bedrooms']) && $Request['bedrooms'] != '') { // Remove "and-more" from the string $filter = str_replace('-and-more', '', $Request['bedrooms']); $bedrooms = array(); if (strpos($filter, 'studio') !== false) { $bedrooms[] = 0; // Remove "studio" from the array if it's combined with other bedrooms $filter = str_replace('studio-and-', '', $filter); } if ($filter != 'studio') { $other_bedrooms = explode('-and-', $filter); $bedrooms = array_merge($bedrooms, $other_bedrooms); } if (in_array(5, $bedrooms)) { $conditions['relation'] = 'OR'; $conditions[] = array( 'key' => 'bedrooms', 'value' => 5, 'compare' => '>=', ); } $conditions[] = array( 'key' => 'bedrooms', 'value' => $bedrooms, 'compare' => 'IN', ); } //set minPrice filter if (isset($Request['minPrice']) && $Request['minPrice'] != '') { $conditions[] = array( 'key' => 'price', 'value' => array($Request['minPrice']), 'compare' => '>=', 'type' => 'NUMERIC', ); } //set maxPrice filter if (isset($Request['maxPrice']) && $Request['maxPrice'] != '') { $conditions[] = array( 'key' => 'price', 'value' => array($Request['maxPrice']), 'compare' => '<=', 'type' => 'NUMERIC', ); } if (isset($Request['sortBy']) && $Request['sortBy'] != '') { $order[] = array( 'orderby_query' => array( 'type' => 'NUMERIC', 'key' => $Request['sortBy'], ), ); } else { $order[] = array( 'orderby_query' => array( 'type' => 'NUMERIC', 'key' => 'price', ), ); } // set neighbourhood filter if (isset($Request['neighbourhood']) && $Request['neighbourhood'] != "monaco") { $neighbourhoods = explode('-and-', $Request['neighbourhood']); $taxConditions[] = array( 'taxonomy' => 'monaco_neighbourhood', 'field' => 'slug', 'terms' => $neighbourhoods, ); } $queryParams = [ 'paged' => $Request["paged"], 'post_type' => 'properties', 'post_status' => 'publish', 'meta_query' => array( 'relation' => 'AND', $conditions, $order, ), 'tax_query' => [ 'relation' => 'AND', [ 'taxonomy' => 'property_type', 'field' => 'slug', 'terms' => ($this->page == "commercial") ? ['commercial-sales', 'commercial-rentals'] : $this->page, ], ], 'posts_per_page' => $postsPerPage, 'orderby' => array( 'orderby_query' => (isset($Request['sortBy']) && $Request['sortBy'] != '') ? 'DESC' : 'ASC', ), ]; if (count($taxConditions)) { $queryParams['tax_query'][] = [$taxConditions]; } $custom_posts = new WP_Query($queryParams); // echo "Last SQL-Query: {$custom_posts->request}"; return $custom_posts; }The page I need help with: [log in to see the link]
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
The topic ‘Advanced WP_Query Issue’ is closed to new replies.