Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Set attached to state

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:

  1. How to change ID of an attachment in wp_posts and its related tables correctly?
  2. How to get custom field image url of specific size
  3. Get the url of the full sized attachment image using post ID?
  4. Set Multiple Meta Values as an Array Using dispatch( ‘core/editor’ ).editPost() Call in Gutenberg/JS
  5. Link Posts to External URL
  6. What snippet do I need to type to show my ACF field show up on my theme?
  7. Is there a way to make [Table Of Content] plugin while not using revision data?
  8. Help to Create a Simple Plugin to make a post
  9. Get value from an input field and pass into update_meta_data as $meta_value
  10. How to force regenerate thumbnails in WordPress posts?
  11. Get post id in Post Page within Wp-admin
  12. WordPress “Add new tag” button is not working
  13. Attachments broken after giving WordPress its own directory
  14. Delete data from custom table when deleting a post
  15. How to auto strip hyperlinks & images in wordpress post
  16. how can authors to define custom pages?
  17. Plugin is creating posts twice
  18. “After file loaded” action
  19. Edit part of a post
  20. How to write a new file when new post has been published in WordPress
  21. WordPress Stats Plugin: Display Post Views [closed]
  22. WP Editor, save content and category on frontend
  23. Plugin for TinyMCE to create reference to other posts
  24. Problem with ajax plugin for refreshing posts
  25. Add posts from other WordPress blog to the current one
  26. How to Get Recent 5 post in My Title bar?
  27. How to get Recent Post From Each Category with Thumbnail?
  28. Plugin for visitors to edit content without logging in? [closed]
  29. How do I allow users to supply a photo/image to be used instead of the grey/white silhouette?
  30. help intercepting save_post through plugin
  31. WP Insert Post If user refreshes override new post
  32. Summary and Comments on Main landing Page
  33. Method/Plugin/Hack to Start a Post with an Writing Outline?
  34. Update existing post dates to random dates and time
  35. Why does “updated_post_meta” did not fire when uploading media other than image?
  36. Why the internal link get converted into a comment?
  37. Is it possible to pin a post in second position from top
  38. Oops! That page can’t be found
  39. page duplication on wordpress that wont stop
  40. Copy/paste local images not working in WordPress
  41. How to change my 3000 Published post status to Draft using PHPMyAdmin
  42. submit two file input fields in the same form
  43. Get all image in media Gallery with alt/title?
  44. New posts are not showing up on my front page
  45. WordPress is redirecting me to homepage
  46. get_post_meta shortcode returns empty field
  47. Is there a hook similar to “save_post” which only fires AFTER the post is completely published?
  48. Adding custom post category to categories widget
  49. How to display the custom post related blog by category?
  50. WP Forms not displaying,becomming an fatal error
  51. Add an action based on custom post meta field
  52. How do I add a medium to the WordPress database with my own plugin?
  53. Randomize post (and page) IDs on generation
  54. Moving all wordpress posts from 50 websites to another website
  55. how to auto random increment for post view
  56. WordPress hide post from custom post-type on a single page
  57. How to securely controlling data without keeping it in postmeta?
  58. How can I see $post object in frontend from functions.php?
  59. Show post object of any page in frontend
  60. .com creates automated link in post
  61. Allow members to create groups on my site
  62. Trying to add a new feature in my wordpress website
  63. Powerpress upload a podcast without creating a post for it
  64. automatically set random featured image by category in wordpress on post
  65. add tags to wordpress post using REST API
  66. “Enable Media Replace” plugin does not update serialized object in WPMeta
  67. Custom Field used to allow a Free Story; no longer works
  68. Is it possible to customize a wordpress post from outside dashboard(Something like site.com/post-type/post/?e=post_id)?
  69. Performance considerations – postmeta table versus new table for custom posts with foreign keys?
  70. Prefix WordPress Taxonomy Tags With Hashtag Symbol Like Twitter
  71. Get and Update Most Meta Value as an array in HTML form
  72. Pagination (including title of each page) in a post for a long posts
  73. How to create meta fields with more than 1 relation?
  74. How to make a page both “private” and “password protected”
  75. Failed to load resource: the server responded with a status of 500 () post.php
  76. How to echo a PHP Code After the Content
  77. All Post WordPress Page Error
  78. WordPress Product Detail and catalog
  79. How to upload PDF from Front-End and post automatically?
  80. Is it possible to create duplicate post on other site (either push, on publish, or pull, periodically)?
  81. How to wrap image tag into div for post only?
  82. How can I get the Post ID and Post Type within a Must Use Plugin?
  83. WordPress CPT Url metabox collection
  84. How to get post that has non zero or greater than zero meta value
  85. Identify if the_post hook is being called from the admin post list
  86. Delete postmeta when uninstall/delete plugin
  87. My own metabox checkbox plugin only saves the last value I’ve checked
  88. Get content and send to 3rd party
  89. How to prevent authors from editing their post count?
  90. Add dynamic color to each category item
  91. Include post_date in search
  92. Same Title on two different post type with single custom taxonomy
  93. Add Filter to get_next_posts_link
  94. Attachments plugin cannot click media library button [closed]
  95. Is there any hook to change media html in post? [duplicate]
  96. Is it possible to get a shortcode generated by a plugin, then, using a function, create a new post using that shortcode?
  97. How to Save settings of custom tab product page in admin side in a database?
  98. How can I make my plugin display custom post meta data on the front end?
  99. How to Insert images in WordPress using code
  100. Displaying friend’s posts only
Categories plugins Tags attachments, plugins, post-meta, posts
current-menu-item not working with custom post type
Display Custom Taxonomy

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress