Skip to content

Instantly share code, notes, and snippets.

@BhargavBhandari90
Created April 26, 2025 05:54
Show Gist options
  • Save BhargavBhandari90/4e1fc563689596e637149ad1cd6fc16b to your computer and use it in GitHub Desktop.
Save BhargavBhandari90/4e1fc563689596e637149ad1cd6fc16b to your computer and use it in GitHub Desktop.
WordPress infinite loop without timeout for doing anything.
<?php
function buntywp_infinite_loop() {
if ( ! is_admin() && isset( $_GET['generate_data'] ) ) {
if ( current_user_can( 'administrator' ) ) {
$page = filter_input( INPUT_GET, 'pg', FILTER_SANITIZE_NUMBER_INT );
if ( empty( $page ) ) {
$page = 1;
}
$limit = 10;
// Query all the replies.
$topic_query = new WP_Query(
array(
'post_type' => 'topic',
'post_status' => 'publish',
'paged' => $page,
'posts_per_page' => $limit,
'order' => 'ASC',
)
);
if ( $topic_query->have_posts() ) {
while ( $topic_query->have_posts() ) {
$topic_query->the_post();
// Do whatever you want to do.
}
wp_reset_postdata();
?>
<script>
window.location.href = '<?php echo site_url() . '/?generate_data=1&pg=' . ( $page + 1 ); ?>';
</script>
<?php
}
} else {
echo 'You are caught!!!! ;) You are DEAD now...!!!!!';
die;
}
}
}
add_action( 'init', 'buntywp_infinite_loop' );
@BhargavBhandari90
Copy link
Author

Follow these STEPS:

  • Add this to your theme's functions.php or a custom plugin's any file.
  • Do your thing in the loop where it says "Do whatever you want to do."
  • and RUN: your_site_url/?generate_data=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment