Skip to content

Instantly share code, notes, and snippets.

@pierre-dargham
Created July 19, 2023 09:28
Show Gist options
  • Save pierre-dargham/9142ecdd4d804edb5ebab1e70094b623 to your computer and use it in GitHub Desktop.
Save pierre-dargham/9142ecdd4d804edb5ebab1e70094b623 to your computer and use it in GitHub Desktop.
scalable-wp-queries.php
<?php
$max_results_per_query = 200;
$offset = 0;
$has_more_results = true;
while($has_more_results) {
$args = [
'post_type' => 'example-post-type',
'post_status' => 'publish',
'posts_per_page' => $max_results_per_query,
'offset' => $offset,
'fields' => 'ids',
'orderby' => 'ID',
'order' => 'ASC',
];
$query = new WP_Query($args);
if(empty($query->posts)) {
$has_more_results = false;
continue;
} else {
$offset += $max_results_per_query;
}
foreach($query->posts as $post_id) {
// don't use models here
$post = get_post($post_id);
$post_meta = get_post_meta($post_id);
// write excel row
\Globalis\WP\Cubi\flush_cache_all();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment