0

So I'm building this post query array:

$posts = get_posts(array(
    'author'        =>  $publisher_info-ID,
    'post_type'         => 'post',
    'posts_per_page'    => 150,
    'meta_query' => array(
    array(
        'taxonomy' => 'type',
        'field'    => 'slug',
        'terms'    => array( 'campaign' ),
    ),
    array(
         'key'     => 'status',
         'value'   => 'active',
        ),
    ),
    
    'orderby'      => 'rand'
));

What I'm struggling is how to integrate two meta key comparison?

Let's say I have two meta keys meta_key A1 and meta_key A2.

Both have numbers in their meta_value.

What I want is to incorporate: A1 < A2 in the array.

Any help would be appreciated.

1
  • 1
    Your meta_query contains a taxonomy query that should be in the tax_query arg, and you're also missing > here: $publisher_info-ID ...
    – Sally CJ
    Commented Sep 15, 2021 at 9:30

1 Answer 1

-1

Use this type of code :

$args = array(
    'author'        =>  $publisher_info->ID,
    'post_type' => 'post',
    'tax_query' => array(
            array(
                'taxonomy' => 'type',
                'field'    => 'slug',
                'terms'    => array( 'campaign' ),
            )
        ),
    'meta_query' => array(
            'relation' => 'AND',
            array(
                    'key' => 'status',
                    'value' => 'active',
                    'compare' => '='),
            array(
                    'key' => 'B',
                    'value' => '1',
                    'compare' => '!=')
            )
);
$data = new WP_Query($args);
2
  • thanks, the array( 'key' => 'status', 'value' => 'active', ), is missing, can you please add that part as well? thanks!
    – robert0
    Commented Sep 15, 2021 at 9:36
  • 1
    I provided you example you change compare meta value according to your need : you change key B and value 1 with your second value which you need to campare with key status and value active. Commented Sep 15, 2021 at 9:59

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.