[wp-trac] [WordPress Trac] #40902: meta_query compare NOT IN with an array of post meta does not work

WordPress Trac noreply at wordpress.org
Thu Jun 1 15:38:09 UTC 2017


#40902: meta_query compare NOT IN with an array of post meta does not work
-----------------------------------------+-----------------------------
 Reporter:  metaphorcreations            |      Owner:
     Type:  defect (bug)                 |     Status:  new
 Priority:  normal                       |  Milestone:  Awaiting Review
Component:  Query                        |    Version:  4.7.5
 Severity:  normal                       |   Keywords:
  Focuses:  administration, performance  |
-----------------------------------------+-----------------------------
 I have a custom meta field called "_tops_ticket_read". This field gets
 added to by using "add_post_meta" and "delete_post_meta", so when using
 "get_post_meta", with the single boolean set to false, it returns an array
 of data. Examples:

 `add_post_meta( $this->get_post_id(), '_tops_ticket_read',
 get_current_user_id() );
 delete_post_meta( $this->get_post_id(), '_tops_ticket_read',
 get_current_user_id() );
 $read_array = get_post_meta( $this->get_post_id(), '_tops_ticket_read' );`

 Now, I'm attempting to use this data to filter a get_posts query by adding
 it to a meta_query. It works as expected when I use the following query
 (using compare=>'IN':

 `$args = array(
   'posts_per_page' => -1,
   'order' => 'ASC',
   'post_type' => 'my_custom_post_type',
   'meta_query' => array(
     'is_read' => array(
       'key' => '_tops_ticket_read',
       'value' => get_current_user_id(),
       'compare' => 'IN'
     )
   )
 );

 $posts = get_posts( $args );`

 This returns all the posts where the current user's ID is contained in the
 array of metadata for "_tops_ticket_read".

 But, if I try to query the posts where the user's ID is not contained in
 the metadata:

 `$args = array(
   'posts_per_page' => -1,
   'order' => 'ASC',
   'post_type' => 'my_custom_post_type',
   'meta_query' => array(
     'is_read' => array(
       'key' => '_tops_ticket_read',
       'value' => get_current_user_id(),
       'compare' => 'NOT IN'
     )
   )
 );

 $posts = get_posts( $args );`

 It returns all the posts instead of just the posts that I would like to
 get.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/40902>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list