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

WP_Query with meta_query returns where zero expected

$
0
0

I’m using a WP_Query with a meta_query key to get results from a custom post type using the following code:

$args = array(
    'post_type'    => 'werknemers',
    'meta_query'   => array(
        'relation' => 'AND',
        array (
            'meta_key'   => 'user',
            'meta_value' => $_POST['user']
        ),
        array (
            'meta_key'   => 'city',
            'meta_value' => $_POST['city']
        )
    )
);

$post = new WP_Query( $args );
var_dump( $post->found_posts );

This code works, and returns all posible results (INT of currently two results). The values entered through $_POST are incorrect so it should return zero (int) results.

I can’t figure out what’s wrong with my code. The strange this is that when I’m using the following arguments it does return zero results (int)..

$args = array(
    'post_type'  => 'werknemers',
    'meta_key'   => 'user',
    'meta_value' => $_POST['user']
);

I guess the problem is somewhere in the relation, but using the documentation I can’t figure out what’s wrong.


Viewing all articles
Browse latest Browse all 36

Trending Articles