I want to extract post related to 3 meta_data fields called bed_value, bath_value, rob_value the variable data is $bedrooms, $bathrooms, &rob, I got some confused with join and like structure. The unworking metaquery is this

$args = array(
    'meta_query' => array(
        'relation' => 'OR',
            'key' => 'beds_value',
            'value' => $bedrooms,
            'compare' => '='  
            'key' => 'baths_value',
            'value' => $bathrooms,
            'compare' => '='    
            'key' => 'rob_value',
            'value' => $rob,
            'compare' => '='    
$args = array_merge( $args , $wp_query->query ); 
query_posts( $args );

So it is better and easier to do a SQL wpdb query I will change the code. prior wpdb query with $wpdb->get_results($query, OBJECT); does not allow to paginate the results, thats why I tried to change to meta_query I need more knowledge!

1 Answer 1


Several things, Firstly you can add the following for pagination:

'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 )

This removes the need for the array_merge. Otherwise your existing code is fine except for some indentation. I would add the rest of the query though, e.g. defining the post type, the posts per page, etc

  • Thanks for this code, fortunatelly the actual code allows to get paged. The main concern on the code above is that only query for one option, some times the user will need to filter data for 2 or 3 options,and now only accepts one filter option; I think is due relation=>'OR' but if I write relation=>'AND' I got no results. So if it easier to do SQL wpdb query which post paginated and filter data for 3 options I'll be happy. also I will bonus via paypal if code is easy to adapt Nov 18, 2011 at 18:37
  • You're using WordPress v3.2.1+ correct?
    – Tom J Nowell
    Nov 19, 2011 at 0:56
  • Yes, this is the latest version to update and updated on dashboard Nov 19, 2011 at 3:46
  • Finally, I found the error... the code to query more than two custom fields are as described above, just is needed to avoid the relation=>'OR' line... Nov 23, 2011 at 23:33
  • The search form is quering 3 fields, which needs to be filled to get the values requested, if one field is empty the query mismatch the results and the results are empty due is quering fields with value " " or null. So now I need to change the compare value from '=' to '!=' automatically if a field request is empty Nov 23, 2011 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.