admin管理员组

文章数量:1289957

I want to use two meta_compare in an array.

// get posts
$posts = get_posts(array(
    'post_type'      => 'post',
    'posts_per_page' => 150,
    'meta_query'     => array(
        array(
            'meta_key'     => 'usp-custom-1',
            'meta_value'   => array('question','money', 'health','relationships'),
            'meta_compare' => 'IN',
            'meta_compare' => '!=',
        ),
    ),
    'author' =>  $_GET["id"],
    'order'  => 'DESC'
));

But it doesn't work.

What I want to achieve is show posts that's NOT equal to these 4 meta_values.

I know I'm probably making a slight mistake somewhere along the lines.

Any help would be appreciated.

I want to use two meta_compare in an array.

// get posts
$posts = get_posts(array(
    'post_type'      => 'post',
    'posts_per_page' => 150,
    'meta_query'     => array(
        array(
            'meta_key'     => 'usp-custom-1',
            'meta_value'   => array('question','money', 'health','relationships'),
            'meta_compare' => 'IN',
            'meta_compare' => '!=',
        ),
    ),
    'author' =>  $_GET["id"],
    'order'  => 'DESC'
));

But it doesn't work.

What I want to achieve is show posts that's NOT equal to these 4 meta_values.

I know I'm probably making a slight mistake somewhere along the lines.

Any help would be appreciated.

Share Improve this question edited Jul 21, 2021 at 10:55 Buttered_Toast 2,8191 gold badge8 silver badges21 bronze badges asked Jul 21, 2021 at 9:40 robert0robert0 2032 silver badges11 bronze badges 7
  • Have you tried using 'meta_compare' => 'NOT IN',? – Buttered_Toast Commented Jul 21, 2021 at 9:44
  • this is going to be a very expensive/heavy/slow query. Post meta tables weren't designed for searches, and asking the database to exclude results is very expensive/slow. usp-custom-1 would make much more sense as a taxonomy, that would mitigate a lot of the performance and scaling issues. – Tom J Nowell Commented Jul 21, 2021 at 9:47
  • Hey Buttered_Toast, for some reason 'meta_compare' => 'NOT IN' still displays posts from all those 4 meta values. – robert0 Commented Jul 21, 2021 at 9:58
  • Tom J Nowell, I agree, but I couldn't think any other way. But I will definitely rethink my strategy, as I have already seen my website to load slower. – robert0 Commented Jul 21, 2021 at 10:01
  • @robert0 when using 'meta_compare' => 'NOT IN', do you still have two meta_compare? because there should be only one, the 'meta_compare' => 'NOT IN' – Buttered_Toast Commented Jul 21, 2021 at 10:04
 |  Show 2 more comments

2 Answers 2

Reset to default 1

Ok so this way is a bit long but it will get the job done.

We can use multiple meta_value checks with AND relation

// get posts
$posts = get_posts(array(
    'post_type'      => 'post',
    'posts_per_page' => 150,
    'meta_query'     => array(
        'relation' => 'AND',
        array(
            'key'     => 'usp-custom-1',
            'value'   => 'question',
            'compare' => '!=',
        ),
        array(
            'key'     => 'usp-custom-1',
            'value'   => 'money',
            'compare' => '!=',
        ),
        array(
            'key'     => 'usp-custom-1',
            'value'   => 'health',
            'compare' => '!=',
        ),
        array(
            'key'     => 'usp-custom-1',
            'value'   => 'relationships',
            'compare' => '!=',
        ),
    ),
    'author' =>  $_GET["id"],
    'order'  => 'DESC'
));

You need to check the performance hit for this query because it can be resouce heavy.

I alos noticed $_GET["id"], you passed it raw, I would suggest sanitizing/validating every value that was passed to you (values that you did not pass yourself, like user input or url queries).

By the property name I assume that it will be a id, so we can sanitize it like this

'author' => filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT)

Looks like without using meta_ prefix, did the job with NOT IN as well:

    // get posts
    $posts = get_posts(array(
    'post_type'         => 'post',
    'posts_per_page'    => 150,
    'meta_query' => array(
        array(
            'key'     => 'usp-custom-1',
            'value'   => array(  'question','money', 'health','relationships'  ),
            'compare' => 'NOT IN',
        ),
    ),
    'author' =>  $_GET["id"],
    'order'             => 'DESC'
));

Thanks to @Buttered_Toast for figuring this out.

本文标签: phpHow to use two metacompare in an array