0

Hi i've looked through the forum but can't seem to find the right solution to what i'm looking

what we have is a property website and have various pages setup to show specific types of properties

for example of page structure Location location / houses location / houses / for sale location / houses / for rent location / apartments etc etc

in the main i have set it all up and its working ok but once the use has drilled down location / houses / forsale it seems to throw up the wrong results

i've setup all the custom meta to accomodate this

<?php
 $args = array(
'post_type' => 'custom_type_houses',
'meta_query' => array(
    'relation' => 'OR',
    array(
        'key' => 'custom_meta_location',
        'value' => 'Arusha',
        'compare' => '='
    ),

    array(
        'key' => 'custom_meta_status',
        'value' => 'For Rent',
        'compare' => '='
    )
)
);
query = new WP_Query( $args );
while($query->have_posts()) {
$query->the_post(); ?>

this works ok however the location comes back as the wrong location i just wanted to check that i'm going about this in the right way and is this the best way of doing it as in is my problem else where?

  • you are never querying any location… that might be the problem… but it looks like a valid custom query. – iantsch Mar 18 '16 at 13:48
  • Query is correct. you may check running the direct query on database which is being return by this WP query. So you can see what DB actually have! – Sumit Mar 19 '16 at 6:00
0

The query as written will retrieve posts with the post type custom_type_houses that are For Rent OR in Arusha. If you want just houses that are both For Rent AND in Arusha you will need to change 'relation' => 'OR' in your meta query to 'relation' => 'AND', like so:

<?php
    $args = array(
        'post_type' => 'custom_type_houses',
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key'     => 'custom_meta_location',
                'value'   => 'Arusha',
                'compare' => '=',
            ),
            array(
                'key'     => 'custom_meta_status',
                'value'   => 'For Rent',
                'compare' => '=',
            ),
        ),
    );

    $query = new WP_Query( $args );

    while ( $query->have_posts() ) {
        $query->the_post();
    }
?>
|improve this answer|||||

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.