Attachments programmatically added to Pages show a 404 error until Page is republished
|Reported by:||bennebw||Owned by:|
When adding attachments to Pages programmatically, they get the status 'inherit', but viewing the Attachment Page returns a 404 error, even if parent Page is Published. Republishing the parent Page fixes the problem for existing attachments, new ones will require republishing before they become active.
This problem does not present when attaching to Posts, only Pages.
It appears that Pages are not allowing attachments to be attached programmatically like Posts do.
Background, this has been tested on 2 separate sites, and error is reproducible. The situation is that images are being uploaded from the blog's front-end (so WP Media Uploader is not being used). They are being attached to either Posts or Pages depending upon situation (Post attachments appear fine, Page attachments require republishing of parent page before attachments can be viewed).
Here is the pertinent code being used for the upload:
$attach_id = wp_insert_attachment( $attachment, $file['file'], (int)$post_id ); $attach_data = wp_generate_attachment_metadata( $attach_id, $file ); wp_update_attachment_metadata( $attach_id, $attach_data ); clean_post_cache( (int)$post_id ); //Added this trying to debug // Also tried wp_update_post() on parent_post, but it did not fix
Ultimately, this prevents the images from being used in a Gallery
Change History (4)
- Keywords needs-patch added; attachments removed
- Milestone changed from Awaiting Review to Future Release
comment:3 markjaquith — 3 years ago
- Milestone Future Release deleted
- Resolution set to worksforme
- Status changed from new to closed