Title: For loop does not do all iterations
Last modified: July 8, 2023

---

# For loop does not do all iterations

 *  [Krishna](https://wordpress.org/support/users/kingswp/)
 * (@kingswp)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/for-loop-does-not-do-all-iterations/)
 * I am trying to create about 500 users in a for loop using wp_insert_user(), but
   the for loop doesn’t loop fully. It sometimes stops after 20 iterations, sometimes
   after 50 items, but never iterates fully.
 * This issue seems to be a general issue with for loop & for each loop in wordpress
   and not about creating users alone as I faced this issue when looping through
   users fetched via WP_User_Query.
 * I have increased the maximum execution time to 60 minutes via set_time_limit(
   60*60), but still no luck.
 * Please let me know how to get for & for each loops to loop through all iterations.
 * I have enabled debug logs and I did not see any errors indicating why it stopped
   iterating abruptly through the for loop.
 *     ```wp-block-code
       // Child theme functions.php
       function some_function() {
       	...
       	wp_schedule_single_event( time() + 10, 'my_hook_create_test_users', $method_args);
       	...
       }
   
       add_action( 'my_hook_create_test_users', 'my_create_test_users', 10, 1 );
   
       function my_create_test_users($method_args) {
       	$default_max_time_limit = ini_get( 'max_execution_time' );
           $time_limit_changed = set_time_limit(60*60);	
           try {
               for($index = 0; $index < 500; $index++) {
                   $userdata = array(
                       'ID' 			=> 0,
                       'user_pass'		=> 'kadjkashdjkad',
                       'user_login' 	=> 'my_test_user_'.$index,
                       'user_email'	=> 'my_test_user_'.$index.'@email.com',
                       'first_name' 	=> 'my_test_user_'.$index.'_FN',
                       'last_name' 	=> 'my_test_user_'.$index.'_LN',
                       'role' 			=> 'subscriber',
                       'meta_input' => array(
                           'my_is_test_user' => 'true',
                           'my_user_type' => 'paid',
                           'my_membership_purchase_date' => time(),
                       )                
                   );
   
                   $userid = wp_insert_user( $userdata );
                   if ( !is_wp_error( $userid ) ) { // check if insert was successful
                       error_log("Test user ".$user->user_email." created.");
                   } else {
                       /* Error Handling */
                       error_log("Exception while creating test user with email: ".$userdata['email']);
                   }        
               }
       	} catch(Exception $e) {
       		error_log("Exception while processing my_create_test_users: ".$e->getMessage());
       	} finally {
               set_time_limit($default_max_time_limit);
           }    
       }
       ```
   

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/for-loop-does-not-do-all-iterations/#post-16880314)
 * Writing to the DB takes a long time relative to how fast PHP executes. PHP has
   enough time, but mySQL queries begin backing up while PHP keeps executing. At
   some point mySQL just stops accepting more queries. It could become so busy that
   it cannot even respond with an error.
 * At least that’s my theory, it’s unconfirmed though. You could add users is small
   batches. After each batch, if there are more to do, schedule a new event to do
   the next batch a while later.
 *  [Lucy Tomas](https://wordpress.org/support/users/lucymtc/)
 * (@lucymtc)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/for-loop-does-not-do-all-iterations/#post-16881740)
 * If it’s an option for your use case you can maybe try running it as a cli command
   [https://make.wordpress.org/cli/handbook/guides/commands-cookbook/](https://make.wordpress.org/cli/handbook/guides/commands-cookbook/)
   
   You can run your script in batches and reset the WordPress database queries and
   object cache on each batch to free up memory.
 *  Thread Starter [Krishna](https://wordpress.org/support/users/kingswp/)
 * (@kingswp)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/for-loop-does-not-do-all-iterations/#post-16886810)
 * Thank you very much [@bcworkz](https://wordpress.org/support/users/bcworkz/) 
   and [@lucymtc](https://wordpress.org/support/users/lucymtc/) for your replies.
 * I had some success with splitting this into small batches.
 * [@lucymtc](https://wordpress.org/support/users/lucymtc/) – Regarding your statement“
   run your script in batches and reset the WordPress database queries and object
   cache on each batch to free up memory”, requesting you to please clarify the 
   below:
    1. **Reset the WordPress database queries**: Why do we need to do this and what
       api can be used to achieve this? Would calling this lead to performance degradation?
    2. **Reset object cache**: Is this to clear the cached results of queries executed
       by WP? And this can be achieved by calling wp_cache_flush() function right? 
       Would calling this lead to performance degradation?
 *  Thread Starter [Krishna](https://wordpress.org/support/users/kingswp/)
 * (@kingswp)
 * [2 years, 10 months ago](https://wordpress.org/support/topic/for-loop-does-not-do-all-iterations/#post-16900509)
 * Dear WP Team – Requesting you to please help clarify my above questions related
   to reset the WordPress database queries and object cache. Thank you
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 10 months ago](https://wordpress.org/support/topic/for-loop-does-not-do-all-iterations/#post-16903854)
 * Lucy stated that the purpose was to free up memory. In the case of doing small
   batches through multiple WP Cron tasks I’m not convinced how effective it would
   be. I’m not familiar with DB server operations, but as far as the WP object cache
   goes, it’s not persistent by default. There is the ability make it persistent
   through caching plugins. In that case wp_cache_flush() would free up memory.
 * For smaller multiple cron tasks it’s probably unnecessary, but if it’s non-persistent
   there wouldn’t be much performance degradation as it’s all stored in relatively
   fast RAM. It might make sense to flush if you’re doing multiple operations in
   a single request.
 * You can reset the DB with [the RESET query](https://dev.mysql.com/doc/refman/8.0/en/reset.html),
   if your user has appropriate privilege. I don’t know how this would affect performance.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘For loop does not do all iterations’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 5 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [2 years, 10 months ago](https://wordpress.org/support/topic/for-loop-does-not-do-all-iterations/#post-16903854)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
