0

I am trying to create some posts using the wp_insert_post. I want all the posts to be parent of the first.

I tried different approaches with no luck. Does anyone have any idea how this could be done?

foreach ( $books as $book ) {

  $post = array(
    'post_title'    => esc_html__( 'Post', 'book-cpt' ),
    'post_content' => 'test',
    'post_status'   => 'publish',
    'post_type' => 'book',
  );

  $book_id = wp_insert_post( $post );

};
1

If I understand you correctly, this should work:

$first_post = true;
$post_parent = 0;

foreach ( $books as $book ) {

  $post = array(
    'post_title'    => esc_html__( 'Post', 'book-cpt' ),
    'post_content' => 'test',
    'post_status'   => 'publish',
    'post_type' => 'book',
    'post_parent' => $post_parent
  );

  $book_id = wp_insert_post( $post );

  if ($first_post) {
    $post_parent = $book_id;
    $first_post = false;
  }    

};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.