In order to fetch Single product to your single.php page you can make use of the code which i have already suggested over here. Refer here:
https://wordpress.stackexchange.com/questions/240653/how-to-query-single-product-in-woocommerce/240726#240726
In order to fetch All products that you have into your DB you can follow the steps below.
You can fetch the Woo-commerce Products based on the WP_Query also so that it is also saved in the wp_posts table alone. So that we might slightly modify the post type alone for getting the WP_Query to work on.
Step One:
First We shall see to the parameters what we have to pass for the WP_Query.
$params = array(
'posts_per_page' => 5,
'post_type' => 'product'
);
Note: As you can see, all we’ve done is added a post_type variable to the array, and set it’s value to "product"; the query will now look for WooCommerce products instead of posts.
Step Two: After that we have to run on with the same structure that the Normal WP_Query behaves.
<?php
$params = array(
'posts_per_page' => 5,
'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(); //Prints the title over Here ?>
<?php //You can add what ever you need from the loop over here ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p>
<?php _e( 'No Products' ); ?>
</p>
<?php endif; ?>
Explanations
- An array of parameters for WP_Query to work with is created; to start with it’s just the basic posts, but by adding more specifics to these parameters we can achieve different results for our queries.
WP_Query is used to query the parameters created in the first line.
- The query is checked to see if it returned any results.
- If there are results, then we iterate over them:
- First, setting the global variable
$post, which ensures that the next function works.
- Second, setting the
the_title() variable, which is responsible for displaying the product title.
- Then, when the posts are displayed, we return the $post variable to its original state with the
wp_reset_postdata function.
Using this link i have studied how to query the WooCommerce Products using WP_Query and I have shared here so that all might get used with this posts.
Happy Coding :)
product_cat?arrayformat and pass it