Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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',

        array(
        'key' => 'beds_value',
        'value' => $bedrooms,
        'compare' => '=',           

        ),
        array(
        'key' => 'baths_value',
        'value' => $bathrooms,
        'compare' => '=',           

        ),
        array(
        '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!

share|improve this question
add comment

1 Answer

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

share|improve this answer
    
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 –  Fernando Baltazar Nov 18 '11 at 18:37
    
You're using WordPress v3.2.1+ correct? –  Tom J Nowell Nov 19 '11 at 0:56
    
Yes, this is the latest version to update and updated on dashboard –  Fernando Baltazar Nov 19 '11 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... –  Fernando Baltazar Nov 23 '11 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 –  Fernando Baltazar Nov 23 '11 at 23:41
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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