Title: Advanced WP_Query Issue
Last modified: August 14, 2023

---

# Advanced WP_Query Issue

 *  [muneeba1](https://wordpress.org/support/users/muneeba1/)
 * (@muneeba1)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/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-type
 *     ```wp-block-code
       public 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](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fadvanced-wp_query-issue%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 1 replies (of 1 total)

 *  Moderator [Tellyworth](https://wordpress.org/support/users/tellyworth/)
 * (@tellyworth)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/advanced-wp_query-issue/#post-16970200)
 * You could use the `NOT EXISTS` operator in a `tax_query` to exclude certain property
   types when the bedrooms filter is active.
 * Surely though the solution here is to fix the incorrect data.

Viewing 1 replies (of 1 total)

The topic ‘Advanced WP_Query Issue’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [Tellyworth](https://wordpress.org/support/users/tellyworth/)
 * Last activity: [2 years, 9 months ago](https://wordpress.org/support/topic/advanced-wp_query-issue/#post-16970200)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
