0

I´ve read almost this articles but unfortunately my query is no working, I think i´m very close but could be so far away.

I´m trying to get metadata using a custom form with method get,

$rob = $_GET['rob'];  
$bedrooms = $_GET['beds'];  
$bathrooms = $_GET['baths'];  

the array is:

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

depending on final query the results are diferent

this display none, seems to be this clean the query

$args = array_merge( $args , $wp_query->query );
query_posts( $args );

but if is used $query = new WP_Query( $args ); I get all results without any filter. but I can see the query data Ex 2 beds, 2 baths

Do I need to write something more?

1
  • you don't need to put quotes round variables when passing them into the query
    – djb
    Commented Jan 18, 2012 at 10:45

1 Answer 1

1

I believe the problem is that you are using single quotes in passing values to query (e.g. 'value' => '$bedrooms'), but this way the variables are not expanded (see http://php.net/manual/en/language.types.string.php). Simply use 'value' => $bedrooms.

9
  • Thanks for answer, I used the valiable without quotes and the results has changed from no results to All results. Now I changed the code 'relation' => 'OR', to 'relation' => 'AND' Now I get filtered some data, but no relations about data query, I mean if selected 2 beds and 2 baths I get information about 2 and 3 beds, and also 1 and 2 baths Commented Nov 14, 2011 at 8:18
  • I think you may omit the 'relation' => 'AND', it's implicit. Moreover if you don't need string pattern matching you may omit the 'compare' => 'LIKE' too and it will uses 'compare' => '=' as default. Please post your actual code to get more help. Commented Nov 14, 2011 at 11:30
  • <pre><code> $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 ); <code> Commented Nov 14, 2011 at 19:55
  • Changing the relation 'relation' => 'OR' to 'relation' => 'AND' I got no results and omiting the line 'relation' => 'OR' also I got no results. So now is querying but only one meta_data but I would like to have querys for 2 or three options... Sorry for distorted code, I can`t put the gray block Commented Nov 14, 2011 at 20:05
  • The problem may be in the way you made the array_merge. I think you should change the order of parameters: array_merge( $wp_query->query , $args ); Commented Nov 15, 2011 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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