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
- WordPress “Add new tag” button is not working
- Attachments broken after giving WordPress its own directory
- Delete data from custom table when deleting a post
- How to auto strip hyperlinks & images in wordpress post
- how can authors to define custom pages?
- Plugin is creating posts twice
- “After file loaded” action
- Edit part of a post
- How to write a new file when new post has been published in WordPress
- WordPress Stats Plugin: Display Post Views [closed]
- WP Editor, save content and category on frontend
- Plugin for TinyMCE to create reference to other posts
- Problem with ajax plugin for refreshing posts
- Add posts from other WordPress blog to the current one
- How to Get Recent 5 post in My Title bar?
- How to get Recent Post From Each Category with Thumbnail?
- Plugin for visitors to edit content without logging in? [closed]
- How do I allow users to supply a photo/image to be used instead of the grey/white silhouette?
- help intercepting save_post through plugin
- WP Insert Post If user refreshes override new post
- Summary and Comments on Main landing Page
- Method/Plugin/Hack to Start a Post with an Writing Outline?
- Update existing post dates to random dates and time
- Why does “updated_post_meta” did not fire when uploading media other than image?
- Why the internal link get converted into a comment?
- Is it possible to pin a post in second position from top
- Oops! That page can’t be found
- page duplication on wordpress that wont stop
- Copy/paste local images not working in WordPress
- How to change my 3000 Published post status to Draft using PHPMyAdmin
- submit two file input fields in the same form
- Get all image in media Gallery with alt/title?
- New posts are not showing up on my front page
- WordPress is redirecting me to homepage
- get_post_meta shortcode returns empty field
- Is there a hook similar to “save_post” which only fires AFTER the post is completely published?
- Adding custom post category to categories widget
- How to display the custom post related blog by category?
- WP Forms not displaying,becomming an fatal error
- Add an action based on custom post meta field
- How do I add a medium to the WordPress database with my own plugin?
- Randomize post (and page) IDs on generation
- Moving all wordpress posts from 50 websites to another website
- how to auto random increment for post view
- WordPress hide post from custom post-type on a single page
- How to securely controlling data without keeping it in postmeta?
- How can I see $post object in frontend from functions.php?
- Show post object of any page in frontend
- .com creates automated link in post
- Allow members to create groups on my site
- Trying to add a new feature in my wordpress website
- Powerpress upload a podcast without creating a post for it
- automatically set random featured image by category in wordpress on post
- add tags to wordpress post using REST API
- “Enable Media Replace” plugin does not update serialized object in WPMeta
- Custom Field used to allow a Free Story; no longer works
- 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?
- Prefix WordPress Taxonomy Tags With Hashtag Symbol Like Twitter
- Get and Update Most Meta Value as an array in HTML form
- Pagination (including title of each page) in a post for a long posts
- How to create meta fields with more than 1 relation?
- How to make a page both “private” and “password protected”
- Failed to load resource: the server responded with a status of 500 () post.php
- How to echo a PHP Code After the Content
- All Post WordPress Page Error
- WordPress Product Detail and catalog
- How to upload PDF from Front-End and post automatically?
- Is it possible to create duplicate post on other site (either push, on publish, or pull, periodically)?
- How to wrap image tag into div for post only?
- How can I get the Post ID and Post Type within a Must Use Plugin?
- WordPress CPT Url metabox collection
- How to get post that has non zero or greater than zero meta value
- Identify if the_post hook is being called from the admin post list
- Delete postmeta when uninstall/delete plugin
- My own metabox checkbox plugin only saves the last value I’ve checked
- Get content and send to 3rd party
- How to prevent authors from editing their post count?
- Add dynamic color to each category item
- Include post_date in search
- Same Title on two different post type with single custom taxonomy
- Add Filter to get_next_posts_link
- Attachments plugin cannot click media library button [closed]
- Is there any hook to change media html in post? [duplicate]
- Is it possible to get a shortcode generated by a plugin, then, using a function, create a new post using that shortcode?
- How to Save settings of custom tab product page in admin side in a database?
- How can I make my plugin display custom post meta data on the front end?
- How to Insert images in WordPress using code
- Displaying friend’s posts only