Created
July 19, 2023 09:28
-
-
Save pierre-dargham/9142ecdd4d804edb5ebab1e70094b623 to your computer and use it in GitHub Desktop.
scalable-wp-queries.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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