I was running into memory issues with this:
get_posts( array( 'posts_per_page' => '-1', 'post_type' => array( 'product', 'product_variation' ) )
(there are thousands of results) just trying to get a list of all post IDs.
The below code grabs 10 products at a time, to hopefully avoid any memory limits. It seems to work OK but there is probably a more elegant solution.
https://gist.github.com/dtbaker/acd15e542d98bff68034
$product_page = 1;
$product_per_page = 10;
$product_query = new WP_Query( array(
'posts_per_page' => $product_per_page,
'paged' => $product_page,
'post_type' => array( 'product', 'product_variation' ),
) );
$product_ids = array();
while($product_query->have_posts() ) {
$product_result = $product_query->next_post();
if(!$product_result){
// get the next lot of results.
$product_page++;
$product_query = new WP_Query( array(
'posts_per_page' => $product_per_page,
'paged' => $product_page,
'post_type' => array( 'product', 'product_variation' ),
) );
}else{
$product_ids[] = $product_result->ID;
}
}
print_r($product_ids);
I would love to know if there’s a better way to do this “10 products at a time” query.
Thanks!