最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wordpress multiple meta value query is not working

matteradmin9PV0评论
$args = array(
    'p'                      => $product_id,
    'post_type'              => array( 'product' ),
    'post_status'            => array( 'publish' ),
    'meta_query'             => array(
        'relation' => 'AND',
        array(
            'key'     => '_ywar_product_id',
            'value'   => $product_id,
            'compare' => '=',
            'type'    => 'NUMERIC',
        ),
        array(
            'key'     => 'recommend_it',
            'value'   => 'yes',
            'compare' => '=',
            'type'    => 'CHAR',
        ),
    ),
);

// The Query
$query = new WP_Query( $args );

echo $query->found_posts;

The result is shown 0 only.

$args = array(
    'p'                      => $product_id,
    'post_type'              => array( 'product' ),
    'post_status'            => array( 'publish' ),
    'meta_query'             => array(
        'relation' => 'AND',
        array(
            'key'     => '_ywar_product_id',
            'value'   => $product_id,
            'compare' => '=',
            'type'    => 'NUMERIC',
        ),
        array(
            'key'     => 'recommend_it',
            'value'   => 'yes',
            'compare' => '=',
            'type'    => 'CHAR',
        ),
    ),
);

// The Query
$query = new WP_Query( $args );

echo $query->found_posts;

The result is shown 0 only.

Share Improve this question edited Mar 5, 2019 at 15:56 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Mar 4, 2019 at 16:20 SAH ALAMSAH ALAM 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

You use the "p"-argument, which looks for the wordpress Post-ID. I guess you want to search the products, that have $product_id in the meta, not for the exact post with the ID $product_id. Just remove the "p" argument from your wp_query arguments.

Post a comment

comment list (0)

  1. No comments so far