1

I have an array of image id's and i want assign them to the specific post:

foreach ($image_ids as  $image_id)
    var_dump(wp_insert_post(array('ID' => $image_id, 'post_parent' => $new_post_id), TRUE));

But there occurs error:

object(WP_Error)#252 (2) {
  ["errors"]=>
  array(1) {
    ["empty_content"]=>
    array(1) {
      [0]=>
      string(38) "Content, title, and excerpt are empty."
    }
  }
  ["error_data"]=>
  array(0) {
  }
}

So is it possible to update the post_parent without updating other data?

2 Answers 2

10

Use wp_update_post(), not insert.

wp_update_post(
    array(
        'ID' => $image_id, 
        'post_parent' => $new_post_id
    )
);
4
  • Thanks a lot. But in codex.wordpress.org/Function_Reference/wp_insert_post is told that if ID exists - it updates current post... Jan 9, 2013 at 22:49
  • Well, yes, the post_parent information is always stored in the child posts, not in the parent. So you have to update the image (post type attachment) to assign it to a parent post.
    – fuxia
    Jan 9, 2013 at 22:53
  • so i did this wp_insert_post(array('ID' => $image_id, 'post_parent' => $new_post_id) Jan 9, 2013 at 23:15
  • 2
    I know that it's old discussion, but if you use wp_insert_post you need to specify all the fields. The fields that aren't in the arguments are updated with the default values. with wp_update_post you just need to specify the fields you want to change and all the other fields will keep the value. Apr 25, 2015 at 13:53
-1

enter image description here

Just you need to give your category or parent name before the postname in Custom Structure section

1
  • How is this related to the question? Oct 21, 2019 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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