#14247 closed defect (bug) (worksforme)
Attachments programmatically added to Pages show a 404 error until Page is republished
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 3.0 |
Component: | Gallery | Keywords: | close |
Focuses: | Cc: |
Description
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)
#1
@
15 years ago
- Keywords needs-patch added; attachments removed
- Milestone changed from Awaiting Review to Future Release
#3
@
14 years ago
- Milestone Future Release deleted
- Resolution set to worksforme
- Status changed from new to closed
Please reopen with full example code (and retest with WordPress 3.1).
#4
@
14 years ago
Have you tried http://goldenapplesdesign.com/2010/07/03/front-end-file-uploads-in-wordpress/ for front-end uploads? It helped me.
Can't reproduce this in 3.0.4 or trunk - using the code supplied above publishes the attachment as expected.