Support

Account

Home Forums Backend Issues (wp-admin) Adding new field to existing posts Reply To: Adding new field to existing posts

  • Check this out:

    $args = array(
      'post_type' => 'product', /* product post type */
      'posts_per_page' => -1 /* all products */
    );
    
    $products = new WP_Query( $args );
    
    // check if products exists
    if( $products->have_posts() ) {
    
      // loop products
      while( $products->have_posts() ): $products->the_post();
    
      // check if field exists
      if( !get_field( 'field_slug' ) ) {
        update_field( 'field_slug' ); /* update field */
      }
      
      endwhile; 
    
      // reset query to default query
      wp_reset_postdata();
    
    }