title: Update wordpress Advance custom field on 40,000 existing posts
body: <p>I have 40000 posts on my website. Yesterday I created a new advanced custom field with a default value. When I open any existing post, The default value does not show up, however when I hit update button or create new post, The default value shows up.</p>
  
  <p>I found a script online to put in function.php file that updates the values automatically. The script is </p>
  
  <pre><code>add_action('admin_init', 'set_default_acf_values');
  
  function set_default_acf_values() {
  
  $args = [
      'post_type'      =&gt; 'chemicals',
      'posts_per_page' =&gt; -1,
  ];
  
     $posts = get_posts($args);
  
     foreach($posts as $post) 
      {
           if (empty(get_field('stock_1', $post-&gt;ID))) {
           update_field('stock_1', DEFAULT_AD_LINK, $post-&gt;ID);
      }
  
  
  
  
  
  }
  }
  </code></pre>
  
  <p>however when I run the script it shows me a memory error.</p>
  
  <pre><code>Your PHP code changes were rolled back due to an error on line 916 of file wp-includes/meta.php. Please fix and try saving again.
  
  Allowed memory size of 805306368 bytes exhausted (tried to allocate 20480 bytes)
  </code></pre>
  
  <p>I believe the reason it is showing memory error because are too many posts.</p>
  
  <p>I also increase memory limit in wp-config.php file, but it did not help at all</p>
  
  <pre><code>define( 'WP_MEMORY_LIMIT', '256M' );
  </code></pre>
  
  <p>What I am looking for is a way to fetch default value from NEW fields on existing posts OR update all existing posts without getting memory error.</p>
  
  <p>any help or pointer would be appreciated a lot.</p>
  
  <p>Thanks</p>