WordPress.org

Make WordPress Core

Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#14247 closed defect (bug) (worksforme)

Attachments programmatically added to Pages show a 404 error until Page is republished

Reported by: bennebw Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0
Component: Gallery Keywords: close
Focuses: Cc:
PR Number:

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 @nacin
9 years ago

  • Keywords needs-patch added; attachments removed
  • Milestone changed from Awaiting Review to Future Release

#2 @solarissmoke
9 years ago

  • Keywords close added; needs-patch removed

Can't reproduce this in 3.0.4 or trunk - using the code supplied above publishes the attachment as expected.

#3 @markjaquith
9 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).

Note: See TracTickets for help on using tickets.