meta_query compare NOT IN with an array of post meta
-
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.
Does anyone have experience in dealing with this?
Thanks!
- The topic ‘meta_query compare NOT IN with an array of post meta’ is closed to new replies.