WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

At the base I have this query wich displays 9 randoms custom post type :

$args= array(
    'post_type' => 'adverts',
    'orderby' => 'rand',
    'posts_per_page' => 9,
    'tax_query' => array(
        array(
            'taxonomy' => 'type',
            'field'    => 'slug',
            'terms'    => $term
        )
    )
);

Now I would like to display 9 adverts (CPT) which have a votes average (meta_key) lower than 6. So I add this :

$args= array(
    'post_type' => 'adverts',
    'orderby' => 'rand',
    'posts_per_page' => 9,
    'meta_query' => array(
        array(
           'key' => 'votes_average',
           'value' => '6',
           'compare' => '<',
           'type'    => 'NUMERIC',
        )
    )
);

When I don't add this meta_query, my first query works. I mean it diplays random posts even those who have a "votes_average" equal to 0. But the secondary query doesn't display the adverts with a "votes_average" equal to 0.

Did I forget something ?

share|improve this question

Finally, this meta_query works :

'meta_query' => array(
            'relation' => 'OR',
            array(
               'key' => 'votes_average',
               'value' => '6',
               'compare' => '<',
               'type'    => 'NUMERIC',
            ),
            array(
                'key' => 'votes_average',
                'compare' => 'NOT EXISTS',
                'value' => 'null',
            )
        ),
share|improve this answer
    
thanks for sharing, works like a charm, indeed! (mind the comma's after 'numeric' and 'null' though...) – ptriek Mar 21 '13 at 15:39
    
Thank you so much for sharing your answer. Works like a charm. – Sodbileg Gansukh Nov 12 '15 at 13:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.