运行“否”LIMIT
查询可能是内存问题的原因。
要获得所有帖子ID的列表,我们必须首先知道存在多少帖子。既然MySQL已经知道这一点,我们可以问问:
$product_query = new WP_Query( array(
\'posts_per_page\' => 1,
\'post_type\' => array( \'product\', \'product_variation\' ),
\'post_status\' => \'publish\', // explicitly setting post_status helps memory usage
\'fields\' => \'ids\',
) );
$total_products_count = $products->found_posts;
此查询将尽可能少地获取帖子总数。
有了这些知识,我们可以使用$total_products_count
我们从上一个查询中发现:
$product_query = new WP_Query( array(
\'posts_per_page\' => $total_products_count,
\'post_type\' => array( \'product\', \'product_variation\' ),
\'post_status\' => \'publish\',
\'fields\' => \'ids\',
\'no_found_rows\' => true, // to disable found_posts calculation
) );
$product_ids = $product_query->posts;