Title: Querying multiple custom fields
Last modified: August 20, 2016

---

# Querying multiple custom fields

 *  [Matthew](https://wordpress.org/support/users/mattycat/)
 * (@mattycat)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/querying-multiple-custom-fields/)
 * I’m having some trouble querying multiple custom fields. I have created a custom
   search form that the values are posted to the query. It’s working, but it’s only
   querying the 1 value. Here is the code:
 *     ```
       <?php
       	$bedrooms = $_GET['bedrooms'];
       	$bathrooms = $_GET['bathrooms'];
       	$bedroom_key = 'bedrooms';
       	$bathroom_key = 'bedrooms'
       ?>
       <?php
       $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       query_posts( array(
       	'post_type' => 'property',
       	'posts_per_page' => '2',
       	'paged' => $paged,
       	'meta_query' => array(
       		array(
       			'key' => $bedroom_key,
       			'value' => $bedrooms,
       			'compare' => '>='
       			),
       		array(
       			'key' => $bathroom_key,
       			'value' => $bathrooms,
       			'compare' => '>='
       			)
       			)
       			) );?>
       ```
   
 * So really, this query is only getting the posts that match the “bedrooms” but
   not the “bathrooms”. This is live at [http://www.bayofplenty.info](http://www.bayofplenty.info)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Erick Hitter](https://wordpress.org/support/users/ethitter/)
 * (@ethitter)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/querying-multiple-custom-fields/#post-2274087)
 * In the outer meta_query array, include ‘relation’ => ‘AND’.
 *  Thread Starter [Matthew](https://wordpress.org/support/users/mattycat/)
 * (@mattycat)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/querying-multiple-custom-fields/#post-2274113)
 * Thanks! I’ve sorted it, it was actually the value of the bathroom_key variable!
   I noticed it had the bedroom value! Such an idiot I am.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Querying multiple custom fields’ is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [meta_query](https://wordpress.org/support/topic-tag/meta_query/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [Matthew](https://wordpress.org/support/users/mattycat/)
 * Last activity: [14 years, 9 months ago](https://wordpress.org/support/topic/querying-multiple-custom-fields/#post-2274113)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
