Quantcast
Channel: Question and Answer » meta-query
Viewing all articles
Browse latest Browse all 36

WP_Query meta query not returning results as expected

$
0
0

I’m trying to run a get_posts query to determine if a users ID is either set as a meta key value or is included within the array of another meta key for an event on a given date.

I am passing $status, $date and $employee_id to this function and have verified that each have the correct value.

$event = get_posts(
    array(
        'post_type'         => 'custom-event',
        'post_status'     => $status,
        'posts_per_page'   => 1,
        'meta_key'       => '_event_date',
        'meta_value'     => date( 'Y-m-d', $date ), // This works OK
        'meta_query'       => array(
            'relation'   => 'OR',
            array( // This works OK
                'key'     => '_artist',
                'value' => $employee_id,
                'compare'  => '=',
                'type'   => 'NUMERIC'
            ),
            array( // This does not work
                'key'     => '_employees',
                'value'     => array( $employee_id ), // Tried passing as array and int
                'compare'  => 'IN'
            ),
        )
    )
);

If $employee_id = 1 and I print the results for a post that has employee_id 1 as the ‘artist’ a post is found.

However if $employee_id = 4 and I print the results no posts are returned. Employee ID 4 is within the _employees meta key and so should be found.

The ‘_employees’ meta key is an array formatted as…
a:2:{i:0;s:1:"4";i:1;s:2:"18";}

Been scratching my head with this for hours now so appreciate any guidance.


Viewing all articles
Browse latest Browse all 36

Trending Articles