Skip to content

Instantly share code, notes, and snippets.

@dtbaker
Created August 26, 2014 04:16
Show Gist options
  • Save dtbaker/acd15e542d98bff68034 to your computer and use it in GitHub Desktop.
Save dtbaker/acd15e542d98bff68034 to your computer and use it in GitHub Desktop.
Get WordPress posts incremently to save on PHP memory usage
// memory friendly alternative to get_posts( array( 'posts_per_page' => '-1' ) );
$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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment