How to display woocommerce product base on it's Id.

Hi,
I have small issue in displaying woocommerce single product base on it's id. i want to show specific products by using it's id. is there any way so i can use it to fetch specific product?

Here is my code.

$my_query = new WP_Query(array( 'post_type' => "product", 'product_id' => 4977 ));
if ( $my_query->have_posts() ):
while ( $my_query->have_posts() ):
$my_query->the_post();
global $product;
the_title();
endwhile; endif;
wp_reset_query();


Please let me know if i did something wrong.


Thank you
Oct 28, 2017
Wordpress Bugs

Experts Replied:

Sensod Developer

Sensod Developer

Replied on: Oct 28, 2017
Hi Ashley,
You need to change ''product_id to 'p' and use it.. See below your updated query.
$my_query = new WP_Query(array( 'post_type' => "product", 'p' => 4977 ));
if ( $my_query->have_posts() ):
while ( $my_query->have_posts() ):
$my_query->the_post();
global $product;
the_title();
endwhile; endif;
wp_reset_query();

Hope this will resolve your problem. Have a nice day.
Thank you
Matthew

Matthew

Replied on: Oct 28, 2017
Hi,
Change your query line with this.
$my_query = new WP_Query(array( 'post_type' => "product", 'p' => 4977 ));
Thank you
Ashley

Ashley

Replied on: Oct 28, 2017
Thank you for your help. Issue resolved.

Topic mark as closed!