-1

I'm trying to query a single product. And I searched it to google on how to do it and a found this code.

do_shortcode('[product_page id="{$product_id}"]');

but this dispaly the whole content of products. All I want is just an image,description and the price. How can I do this using WP_QUERY()?

closed as off-topic by TheDeadMedic, cjbj, Mark Kaplun, Dave Romsey, Howdy_McGee Sep 28 '16 at 18:46

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

3

If you want to use the Wp_Query for selecting the single product from the database you have to pass the p in the $args so that if fetches the data and the post_type should be as product.

Follow up the Normal $args method along with that you have to add the following lines of code.

<?php
$params = array(
 'p' => 'YOUR PRODUCT ID', id of a page, post, or custom type
 'post_type' => 'product'
);
$wc_query = new WP_Query($params); 
?>
<?php if ($wc_query->have_posts()) :  ?>
<?php while ($wc_query->have_posts()) : $wc_query->the_post();  ?>
<?php the_title();  ?>
<?php the_content();  ?>
<?php endwhile; ?>
<?php wp_reset_postdata();  ?>
<?php else:  ?>
<p><?php _e( 'No Product' );  ?></p>
<?php endif; ?>
  • thanks for this. I'm new in wordpress. I dont have enough knowledge on it. – user3818576 Sep 28 '16 at 0:31
  • Its my pleasure helping you. Happy coding:) – Naresh Kumar P Sep 28 '16 at 1:32
-1

Why not just use the shortcode that provides WooCommerce by default? I think is more practical than create another separated code or function to do the same stuff.

[product id="99"]

Check out it here : Shortcodes included with WooCommerce

Well, with a variable, you can try this in your template:

<?php echo do_shortcode('[product_page id='.$product_id.']'); ?>
  • he is asking about wp_query code... – Mark Kaplun Sep 27 '16 at 15:18

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