wordpressmeta-query

Find other product with exact same title


Following code get products I need but it does not match exact tile (name), so sometime it get some unwanted product.

$pname = $product->get_name();

$args = array(
 'post_type' => 'product',
 'post_status' => 'publish',
 'posts_per_page' => '5',
 'order' => 'asc',
 "s" => $pname,

 'tax_query' => array(
   array(
    'taxonomy' => 'product_type',
    'field' => 'slug',
    'terms' => 'bundle',) )
);

$products = new WP_Query($args);

Now if I add 'meta_qury' to it, it does not work at all, page shows error.

$args = array(
 'post_type' => 'product',
 'post_status' => 'publish',
 'posts_per_page' => '5',
 'order' => 'asc',
 "s" => $pname,

  'tax_query' => array(
   array(
    'taxonomy' => 'product_type',
    'field' => 'slug',
    'terms' => 'bundle',) )

  'meta_query' => array(
   array(
    'key' => 'title',
    'value' => $pname,
    'compare' => '=') )
);

Can someone please help me out with this. I'm new to WordPress and PHP.


Solution

  • It was quite simple, just changing

    "s" => $pname,
    

    to

    'title' => $pname,
    

    in original code without adding any 'meta_query' solved the issue.