$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>WP Meta query for a custom post type based on two values|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

WP Meta query for a custom post type based on two values

matteradmin9PV0评论

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: /

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

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

Share Improve this question edited Nov 28, 2018 at 11:27 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Nov 28, 2018 at 10:25 VitalyVitaly 132 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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'),
        )
    ) 
);
Post a comment

comment list (0)

  1. No comments so far