Reputation: 27
I would like to ask how is it possible when i have 3000 variable products in an old woocommerce version eshop, when i am trying to loop throught them that i get only 300 in return instead.
The following code is used by me in order to generate a product feed.
$args = array( 'post_type' => 'product');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
Also the variable products are called variable because they support variation as I understand. That means that 3000 variable products are not 3000 "normal" products and instead the 300 that I see is the correct number of them?
Upvotes: 1
Views: 283
Reputation: 253949
May be you are mixing up products (simple or variable) and product variations (that remain to variable products)... You need to display products (simple and variable) in your WP_Query
, but not your product variations.
With 'post_type' => 'product'
, you will get simple and variable products at the same time.
The missing argument is posts_per_page
to be set to -1
.
So your code will be:
$args = array( 'post_type' => 'product', 'posts_per_page' => -1);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) :
$loop->the_post();
// set all product IDs in an array
$all_product_ids[] = $loop->post->ID;
endwhile;
// Testing: Output the number of products in that query
echo '<p>Products count: ' . count($all_product_ids) . '</p>';
Then you will get all your products.
May be you can try to use a WPDB query:
## --- THE SQL QUERY --- ##
global $wpdb;
$table_name = $wpdb->prefix . "posts";
$product_ids_array = $wpdb->get_col("
SELECT `ID`
FROM `$table_name`
WHERE `post_status` LIKE 'publish'
AND `post_type` LIKE 'product'
");
## --- TESTING OUTPUT --- ##
// Testing raw output of product IDs
print_r($product_ids_arr);
// Number of products
$products_count = count($product_ids_arr);
echo '<p>Products count: '. $products_count. '</p>';
## --- THE LOOP --- ##
foreach ($product_ids_array as $product_id):
// Get an instance of WP_Product object
$product = new WC_Product($product_id);
// Use the WP_Product methods to get and output the necessary data
endforeach;
Upvotes: 1