WP_Query eliminate memory overflow
Whenever WP_Query() runs on big blogs with many posts, it takes lot of memory, so we get memory overflow.
$args = array( 'post_status' => 'publish', 'post_type' => array( 'post' ), 'posts_per_page' => -1 ); $query = null; wp_reset_query(); $query = new WP_Query($args);
Why there is Memory Overflow?
WP_Query actually loads every matching post into memory, including the full post contents; and how smart WordPress is; it may also load meta data for every post as pre-fetch action. This easily leads to the memory overflow.
But there is a million dollars trick, where you pass 'fields' => 'ids'
into WP_Query to simply return a list of IDs.
$args = array( 'post_status' => 'publish', 'post_type' => array( 'post' ), 'posts_per_page' => -1, 'fields' => 'ids' ); $query = null; wp_reset_query(); $query = new WP_Query($args);
Now, how is that for a memory trick.
…
tags: & category: -