WordPress.org

Forums

[resolved] use query_posts get posts by multiple custom fields (4 posts)

  1. hermitjim
    Member
    Posted 2 years ago #

    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.

  2. prionkor
    Member
    Posted 2 years ago #

    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' => '='
    	);
  3. hermitjim
    Member
    Posted 2 years ago #

    Aaaah of course, I am a fool, thank you so much!!

  4. hermitjim
    Member
    Posted 2 years ago #

    resolved

Topic Closed

This topic has been closed to new replies.

About this Topic