Support » Fixing WordPress » Attachment Parent

  • You start to “Write [a new] Page”. You put in a title, add some text… then you “Upload” a file and send it to the editor.
    You “Publish” your newly created page.

    The attachment has -xxxxxxx as post_parent.
    Isn’t that weird?!

    I’m using WordPress as a secondary interface, and linkage is essential (I need to see what files have been uploaded/attached to each and every post).

    Why isn’t post_parent update let’s say.. upon sending it to editor, in case it doesn’t already have a parent?? Or even upon saving the current post??

    Why do I have to write a title… then Save, then Upload, then write the text?!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Bumping this topic up because I just encountered the same thing in WP 2.2.1.

    I have some code that takes a post, looks to see if it has children of type ‘attachment’ and then does stuff with it. This means that the post_parent relationship between an uploaded file and the post it was uploaded to is very important to the success of my code.

    However, I have noticed that I can successfully ‘attach’ a file to a post by uploading it. However, if I go back to that file (in the Browse tab of the parent post) and edit its Title or Description, suddenly the connection between the attachment and the parent post is gone. I checked the database and the post_parent field for the attachment post is set to 0 instead of the correct post_parent id.

    So it seems that the initial upload correctly sets the post_parent for the attachment post, but any subsequent changes breaks it. Can we have a fix in the next patch?

    As for temporary solutions, I’m considering using a Hook/Filter approach if possible.

    I’m having this same issue, and as with wonkavator, this is a very important feature for a lot of my custom code — the parent/child relationship has to be maintained. Any help with this greatly appreciated.

    I have a solution that seems to work fine for this. I haven’t tested it too heavily.

    in wp-includes/post.php
    look for function wp_update_post
    on line 747 (for me, wp v. 2.2.1) you’ll see this:

    if ($postarr[‘post_type’] == ‘attachment’)
    return wp_insert_attachment($postarr);

    change to this:

    if ($postarr[‘post_type’] == ‘attachment’)
    return wp_insert_attachment($postarr, false, $post[‘post_parent’]);

    hope this helps!


    Grate… That fixed my problems.


Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Attachment Parent’ is closed to new replies.