Title: use query_posts get posts by multiple custom fields
Last modified: August 20, 2016

---

# use query_posts get posts by multiple custom fields

 *  Resolved [hermitjim](https://wordpress.org/support/users/hermitjim/)
 * (@hermitjim)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/use-query_posts-get-posts-by-multiple-custom-fields/)
 * Hi, I’ve hunted for an answer to this and haven’t found one that works. Trying
   to make sense of someone-else’s code. I’m trying to show posts by author ID, 
   post type=private, and two custom fields ‘paid_user’ and ‘txrole’. The code below
   is just returning all private posts from all users, and not differentiating between
   the custom fields. What am I doing wrong? Thank you for your help.
 *     ```
       <?php
   
       	global $wp_query;
       	$query_vars = $wp_query->query_vars;
       	$post_per_page = 20; 
   
       	global $current_user;
       	get_currentuserinfo();
       	$uid = $current_user->ID;
   
       	$winner = array(
       	'meta_key' => 'winner',
       	'value' => $uid,
       	'compare' => '='
       	);
   
       	$paid = array(
       	'meta_key' => 'paid_user',
       	'value' => "0",
       	'compare' => '='
       	); 
   
       	$tx_role = array(
       	'meta_key' => 'txrole',
       	'value' => "bought",
       	'compare' => '='
       	); 
   
       	$args = array(
       	'post_status' => 'private',
       	'post_author'=>$uid,
       	'order' => 'DESC',
       	'orderby' => 'date',
       	'posts_per_page' => $post_per_page,
       	'pages' => $query_vars['paged'],
       	'meta_query' => array($winner, $paid, $tx_role));
   
       	query_posts($args);
   
       	if(have_posts()) :
       		while(have_posts()) : the_post();
       			get_backoffice();
       		endwhile;
   
       		if(function_exists('wp_pagenavi')): wp_pagenavi(); endif;
       	else: _e("None",'AuctionTheme');
   
       	endif;
   
       	wp_reset_query();
       ?>
       ```
   
 * PS – get_backoffice() is just a function to format the results, which is working
   fine elsewhere.

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

 *  [prionkor](https://wordpress.org/support/users/prionkor/)
 * (@prionkor)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/use-query_posts-get-posts-by-multiple-custom-fields/#post-3055560)
 * [http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters](http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters)
 * When the array key inside `meta_query` is only called `key` not `meta_key`. So,
 * **This:**
 *     ```
       $tx_role = array(
       	'meta_key' => 'txrole',
       	'value' => "bought",
       	'compare' => '='
       	);
       ```
   
 * **Will be look like this:**
 *     ```
       $tx_role = array(
       	'key' => 'txrole', // changing "meta_key" to "key"
       	'value' => "bought",
       	'compare' => '='
       	);
       ```
   
 *  Thread Starter [hermitjim](https://wordpress.org/support/users/hermitjim/)
 * (@hermitjim)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/use-query_posts-get-posts-by-multiple-custom-fields/#post-3055562)
 * Aaaah of course, I am a fool, thank you so much!!
 *  Thread Starter [hermitjim](https://wordpress.org/support/users/hermitjim/)
 * (@hermitjim)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/use-query_posts-get-posts-by-multiple-custom-fields/#post-3055722)
 * resolved

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

The topic ‘use query_posts get posts by multiple custom fields’ is closed to new
replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [hermitjim](https://wordpress.org/support/users/hermitjim/)
 * Last activity: [13 years, 7 months ago](https://wordpress.org/support/topic/use-query_posts-get-posts-by-multiple-custom-fields/#post-3055722)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
