• 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.
Viewing 2 replies - 1 through 2 (of 2 total)
  • 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.

    @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.