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 );
Related Posts:
- How to change ID of an attachment in wp_posts and its related tables correctly?
- How to get custom field image url of specific size
- Get the url of the full sized attachment image using post ID?
- Set Multiple Meta Values as an Array Using dispatch( ‘core/editor’ ).editPost() Call in Gutenberg/JS
- Link Posts to External URL
- What snippet do I need to type to show my ACF field show up on my theme?
- Is there a way to make [Table Of Content] plugin while not using revision data?
- Help to Create a Simple Plugin to make a post
- Get value from an input field and pass into update_meta_data as $meta_value
- How to force regenerate thumbnails in WordPress posts?
- Get post id in Post Page within Wp-admin
- How Do I Use The WordPress Plugin Posts 2 Posts by Scribu?
- publish_post hook isn’t working for scheduled posts
- Plugin for changing a post’s category based on it’s post date?
- How can I get an image from the uploads dir and enter it in the media library? [closed]
- Publish Post After Click On A Link
- Enable comments for post with comments meta box removed
- Edit/revise option for authors to suggest improvement to posts [closed]
- Is there an action that is called when a post is restored from the trash?
- plugin for wp_list_categories with posts
- Action on post publish
- Replace existing pages with new pages, keep menu links
- Adding “ ” before the last word in multiple defined areas with a plugin
- [Plugin: Posts 2 Posts] How does it work?
- Use plugin or custom post type for game score functionality
- How to add new tabs in post.php
- How can I get full attachment url from wp_get_attachment_metadata?
- Converting Attachment to Images?
- Bulk Image Uploader to create new post from each image [closed]
- Display comments of users on single page
- Using the “Latest posts” feature on a different site
- Post query – show posts from specified day and month and whole years
- Where to hook my plugin’s action
- Is there a way to order posts and custom post types as one group?
- Is there a filter for get_post_custom()?
- How can i list random post from multiple category?
- Support for simultaneous editing
- dynamically generating plugin syntax
- Accessing post’s meta data based on user’s click of a post
- Is there an easy way to flag posts in the admin area?
- finding whether request is for post, and post id
- How to allow an editor to edit all WordPress blog posts but have the last say as administrator?
- Trying to fix form resubmition with PRG (Getting error: Cannot modify header information – headers already sent by)
- WordPress Post Visibility Options for Frontend
- Display wordpress post’s in popup?
- Bulk update all posts from plugin settings screen
- Unable to add TAB character to post?
- how to add text to posts from tags
- How to insert multiple postmeta values with one query?
- How to get the first post and second post in a wordpress blog?
- ‘Organize Series Plugin’ as muti author feature
- Mass SQL WordPress Meta Key deletion
- Date format – Meta Box plugin
- get meta value from database by post id
- Is there a way to make the main page only display a brief description of the full article?
- WordPress plugin Post Creation [closed]
- posts from multiple post types in one slider
- How to display the featured post on the category page?
- How can I concatenate variables to search results title? [closed]
- Embed PDF into wordpress
- WordPress plugin: Getting posts where checkbox selected
- Remember the Meta Value With Radio Buttons
- Attachments broken after giving WordPress its own directory
- How to auto strip hyperlinks & images in wordpress post
- WP Editor, save content and category on frontend
- Plugin for TinyMCE to create reference to other posts
- How to get Recent Post From Each Category with Thumbnail?
- help intercepting save_post through plugin
- Why the internal link get converted into a comment?
- Copy/paste local images not working in WordPress
- How to change my 3000 Published post status to Draft using PHPMyAdmin
- get_post_meta shortcode returns empty field
- Add an action based on custom post meta field
- how to auto random increment for post view
- .com creates automated link in post
- Is it possible to customize a wordpress post from outside dashboard(Something like site.com/post-type/post/?e=post_id)?
- Performance considerations – postmeta table versus new table for custom posts with foreign keys?
- All Post WordPress Page Error
- Is it possible to create duplicate post on other site (either push, on publish, or pull, periodically)?
- Delete postmeta when uninstall/delete plugin
- Add dynamic color to each category item
- Attachments plugin cannot click media library button [closed]
- Is there any hook to change media html in post? [duplicate]
- Publish Post but don’t call publish hook in foreground post creation
- Better code/text editing for posts and pages?
- Differentiate attachment pages in search results
- Style WordPress Custom Post Type Single Page
- SWF in wordpress post
- How to show post attachment image
- get post excerpt by query
- Using Postmash plugin in WordPress 3.3.1
- Add meta tags with a plugin?
- What is the correct method for updating post content from a plugin?
- WordPress User Frontend Editing Custom Fields
- PHP $_session is not work in wordpress
- Next and Previous Pagination button not displaying in WordPress
- Sell “create a post”, paid account for post creation
- Shortcode returning specific content of a post
- Looking for membership style plugin with edit option [closed]
- Getting 400 Error in wordpress website