0
$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.

1 Answer 1

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.

4
  • I remove the "p" argument but still show the same result 0. Commented Mar 4, 2019 at 18:13
  • did you check that there are posts of the post type "product" having the specified meta keys/values? Commented Mar 5, 2019 at 7:22
  • Post type 'product' is in posts table and it links up by product id and meta key is in post meta table. drive.google.com/file/d/1mk4Qyn-XVRMGPTSJrPibY1igxgGCCHtB/… Commented Mar 5, 2019 at 10:41
  • What i asked is, do these two criteria meet at all in your database? Your screenshot shows that the post with the id 343 has the "recommend_it" meta value, but not the "_ywar_product_id" meta_value; the post with the id 344 has the "_ywar_product_id", but no "recommend_it". The Query you posted asks for posts which have as well the meta value of $product_id in the meta_key "_ywar_product_id" AND the value "yes" in the meta_key "recommend_it". If there are no posts that have both of that, the query will return 0. Commented Mar 5, 2019 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.