Title: wp_insert_post
Last modified: June 28, 2020

---

# wp_insert_post

 *  [webdesign8287](https://wordpress.org/support/users/webdesign8287/)
 * (@webdesign8287)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/wp_insert_post-7/)
 * am I using the wp_insert_post() function to create posts within a loop.
    The 
   code works correctly, however, it duplicates the inserts.
 * my code is in following:
    `$product = wc_get_product($product_id); $variation_post
   = array( ‘post_title’ => ‘Variation #’ . $product_id, ‘post_name’ => ‘product-‘.
   $product_id .’-variation’, ‘post_status’ => ‘publish’, ‘post_parent’ => $product_id,‘
   post_type’ => ‘product_variation’, ‘guid’ => home_url() . ‘/?product_variation
   =product-‘ . $product_id . ‘-variation’ );
 *  $variation_id = wp_insert_post( $variation_post );
    -  This topic was modified 5 years, 11 months ago by [webdesign8287](https://wordpress.org/support/users/webdesign8287/).

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

 *  [corrinarusso](https://wordpress.org/support/users/corrinarusso/)
 * (@corrinarusso)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/wp_insert_post-7/#post-13045842)
 * I’ve not seen this syntax before :
    `'guid' => home_url() . '/?product_variation
   =product-' . $product_id . '-variation'`
 * Why are you passing a full path URL into the guid ?
 * What does variation_id do ?
    `$variation_id = wp_insert_post( $variation_post);`
 * If these arguments are valid to wp_insert_post, then I would guess that the error
   is in your loop. But you did not provide that.
 *  [Matt Robinson](https://wordpress.org/support/users/mattyrob/)
 * (@mattyrob)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/wp_insert_post-7/#post-13045940)
 * [@webdesign8287](https://wordpress.org/support/users/webdesign8287/)
 * If you are looping through and calling that code I would suspect that the issues
   is that your loop is not incrementing the $product correctly – where are you 
   getting $product_id from and how are you incrementing it?

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

The topic ‘wp_insert_post’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 3 participants
 * Last reply from: [Matt Robinson](https://wordpress.org/support/users/mattyrob/)
 * Last activity: [5 years, 11 months ago](https://wordpress.org/support/topic/wp_insert_post-7/#post-13045940)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
