WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#40902 closed defect (bug) (worksforme)

meta_query compare NOT IN with an array of post meta does not work

Reported by: metaphorcreations Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.7.5
Component: Query Keywords:
Focuses: administration, performance Cc:
PR Number:

Description

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.

Change History (3)

#1 follow-up: @adam3128
2 years ago

  • Resolution set to worksforme
  • Status changed from new to closed

Hey metaphorcreations,

I can't imagine this being a bug straight off the bat.

Otto does a good job of explaining what you might be facing over at

https://wordpress.stackexchange.com/questions/66056/filter-posts-with-meta-query-not-in-where-value-has-multiple-values

In short, I'd probably check that you don't have multiple meta rows, change your value parameter to an array and check posts contain the meta field with a value.

Hope this helps.

#2 in reply to: ↑ 1 @metaphorcreations
2 years ago

I don't think this needs to be continued here, but could you take a look at the support forum ticket I made https://wordpress.org/support/topic/meta_query-compare-not-in-with-an-array-of-post-meta/#post-9189125

I'm interested to hear your thoughts on what I'm attempting to do. Thanks!

Replying to adam3128:

Hey metaphorcreations,

I can't imagine this being a bug straight off the bat.

Otto does a good job of explaining what you might be facing over at

https://wordpress.stackexchange.com/questions/66056/filter-posts-with-meta-query-not-in-where-value-has-multiple-values

In short, I'd probably check that you don't have multiple meta rows, change your value parameter to an array and check posts contain the meta field with a value.

Hope this helps.

#3 @netweb
2 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.