1

I'm hoping you can help me out and give me a tip on something I'm trying to achieve and failing miserably.

I'm working on a WordPress real-estate theme - Real homes. Here is the link to the website: https://lnobrerealestate.pt/

What I'm trying to achieve is to have two sections with featured properties, one for sales and one for rentals.

So in order to do that, I need to change the query that gets the posts. The original query that gets a list of featured properties is:

/* Featured Properties Query Arguments */
$featured_properties_args = array(
    'post_type'         => 'property',
    'posts_per_page'    => 12,
    'meta_query'        => array(
        array(
            'key'       => 'REAL_HOMES_featured',
            'value'     => 1,
            'compare'   => '=',
            'type'      => 'NUMERIC'
        )
    )
);

This query gets the first 12 properties that are marked as featured. What I need to add is another condition that would check if the property status is "For Sale" or "For Rent".

I can get an array of property statuses with this query:

        $property_status_terms = get_terms(array(
                'taxonomy'   => "property-status",
                'orderby'    => 'name',
                'order'      => 'ASC',
                'hide_empty' => false
            )
        );

        if (! empty($property_status_terms)) {
            foreach ($property_status_terms as $property_status) {
                echo '<p>' . $property_status->name . '</p>';
            }
        }

So I though that based on these two queries I can do the following:

/* Featured Properties Query Arguments */
    $featured_properties_args = array(
        'post_type'         => 'property',
        'posts_per_page'    => 12,
        'meta_query'        => array(
            'relation' => 'AND',
            array(
                'key'       => 'REAL_HOMES_featured',
                'value'     => 1,
                'compare'   => '=',
                'type'      => 'NUMERIC'
            ),
            get_terms(array(
            'taxonomy'       => 'property-status',
            'value'     => array('For Rent', 'Arrendamento'),
            'compare'   => 'IN',
             )
             )
        )
    );

I'm comparing the array of property statuses with two values - for rent in case of English and Arrendamento in case of Portuguese.

This query doesn't result in what I'm trying to achieve and I can't get what I'm missing here. Seems quite straight forward.

I'm hoping you can give me a suggestion.

Thanks

0

OK, so that's a little example of what I call "wishful coding" ;)

get_terms(array(
    'taxonomy'       => 'property-status',
    'value'     => array('For Rent', 'Arrendamento'),
    'compare'   => 'IN',
) )

You use get_terms function here, which will

Retrieve the terms in a given taxonomy or list of taxonomies.

And you use this inside of meta_query part of a query, which is used to query custom fields.

So there is no way it could do anything at all... Terms are not valid meta_query queries...

But of course it can be done... All you need to do is to check docs for WP_Query...

You already have this:

$featured_properties_args = array(
    'post_type'         => 'property',
    'posts_per_page'    => 12,
    'meta_query'        => array(
        array(
            'key'       => 'REAL_HOMES_featured',
            'value'     => 1,
            'compare'   => '=',
            'type'      => 'NUMERIC'
        )
    )
);

and you know it gets you featured properties. So all you have to do is to add your tax query in there:

$featured_properties_args = array(
    'post_type'         => 'property',
    'posts_per_page'    => 12,
    'meta_query'        => array(
        array(
            'key'       => 'REAL_HOMES_featured',
            'value'     => 1,
            'compare'   => '=',
            'type'      => 'NUMERIC'
        )
    ),
    'tax_query'         => array(
        array(
            'taxonomy'  => 'property-status',
            'field'     => 'name',
            'terms'     => array('For Rent', 'Arrendamento'),
        )
    ) 
);
|improve this answer|||||
  • 1
    Thank you so much for helping, it worked perfectly. I was so close but yet so far away :) – Vitaly Nov 28 '18 at 11:48

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.