Leave a Reply

1 comment

  1. Attachments are attached to a parent post. So when you get an attachment object, look into $attachment->post_parent. If that is 0, the file is not attached.

    The parent post ID refers always to another post in the posts table, never to an user. To attach the file to a user, you could create a hidden custom post type and one entry per user. But I don’t think it is worth the efforts. There is nothing wrong with unattached files.

    To change the post_parent value use wp_update_post().

    Pseudo-code, not tested:

    $att = get_post( $attachment_id );
    $att->post_parent = $new_parent_id;
    wp_update_post( $att );