Make an array for a query with false conditions

Asked

Viewed 55 times

1

I have the following query, where the conditions are informed from a array:

$args = array(
    'post_type'           => 'post',
    'post_status'         => 'publish',
    'orderby'             => 'date',
    'order'               => 'DESC',
    'ignore_sticky_posts' => true,
    'category_name'       => $atts['category_name'],
    'posts_per_page'      => ($atts['number'] > 0) ? $atts['number'] : 
                              get_option('posts_per_page')
 );

 $meta_query[] = array(
    'key'   => '_featured',
    'value' => 'yes' 
 );

 $args['meta_query'] = $meta_query;

 $the_query = new WP_Query($args);

That one query brings me the posts which are marked as Featured. But what I need is the reverse, I need the posts who are not Featured. If I just put 'no' in place of 'yes', in that passage:

'key'   => '_featured',
'value' => 'yes'

He only returns to me posts that once were featured. But those who have never been, do not appear, because they neither possess the meta_key featured.

The easiest thing would be to bring the posts different from "featured = yes", but I don’t know how to do it.

  • And if you leave the 'Value' => '' ??

  • Or something like that: $meta_query[] = array(
 'key' => '_featured',
 'key.count' => 0,
 'value' => 'no' 
 );

  • if I leave it: 'Value' => '' returns nothing.

2 answers

2


Beyond the elements key and value, to meta_query supports the comparison operator in the element compare. Try !=:

 $meta_query[] = array(
    'key'     => '_featured',
    'value'   => 'yes' ,
    'compare' => '!='
  );

There is a lot of information on Codex, and found a tutorial more "chewed" here. A search for "meta_compare" shows that the possible values are those supported by Mysql:

'=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'NOT EXISTS', 'REGEXP', 'NOT REGEXP' or 'RLIKE'

The standard being the equal operator '='.

For more information on the usefulness of each operator, mysql documentation is well detailed.

0

Thank you guys, you’ve been very helpful!

The code that worked was as follows:

$meta_query[] = array(
   'relation' => 'OR',
   array (
         'key'   => '_featured',
         'compare' => 'NOT EXISTS'
         ),
   array (
         'key'   => '_featured',
         'value' => 'no' 
         )  
);
  • Doesn’t it get ambiguous like this Nathalia? Can’t put the 'value' => 'no' within the first array ?

  • 1

    It doesn’t, because it’s two kinds of posts. There are posts that have the _Feature = no (these one day were "yes") and there are posts q do not have the _Feature parameter.

  • Yeah, all right, is that those arrays there are to which the Wordpress mount the SQL, then as the query will be only one, I believe it will give in the same if you put inside an array only, no ?

  • 1

    From what I read in the tutorial posted by nunks.lol here, 2 arrays are needed to add OR.

  • Ahh yes, I had not seen the relationship there, then yes it makes sense. My ' D failure

Browser other questions tagged

You are not signed in. Login or sign up in order to post.