WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In the media library every attachment post has an "Attached to" column. I've developed a plugin that adds author profile and a facebook like banner image.

I now discovered that the attachment posts, that are used as profile pictures, are not marked as "attached to".

I used a wordpress console plugin to get WP_Post objects of a linked and of an unlinked attachment. But I couldn't find any difference between them that I could use to set it manually.

Is there any way to set the attached ststus of attachment posts?

share|improve this question
up vote 1 down vote accepted

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 );
share|improve this answer
Ah. Okay, I see, thank you! Would there be a possibility to link an attachment to an author? – Julian F. Weinert Apr 22 '13 at 21:07
Not really. The post parent field is always just an integer, and that will be looked up in the posts table. – toscho Apr 22 '13 at 21:08
@Julian, you could then attach your authors to a CPT. – brasofilo Apr 22 '13 at 21:10
@Julian See my update. – toscho Apr 22 '13 at 21:14
I was afraid of that. I searched the hooks list, but couldn't find anything matching. Do you eventually know a hook that could be used to modify this information? – Julian F. Weinert Apr 22 '13 at 21:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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