$args = array(
    'p'                      => $product_id,
    'post_type'              => array( 'product' ),
    'post_status'            => array( 'publish' ),
    'meta_query'             => array(
        'relation' => 'AND',
            'key'     => '_ywar_product_id',
            'value'   => $product_id,
            'compare' => '=',
            'type'    => 'NUMERIC',
            '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.


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.

| improve this answer | |
  • I remove the "p" argument but still show the same result 0. – SAH ALAM Mar 4 '19 at 18:13
  • did you check that there are posts of the post type "product" having the specified meta keys/values? – HU ist Sebastian Mar 5 '19 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/… – SAH ALAM Mar 5 '19 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. – HU ist Sebastian Mar 5 '19 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.