Make WordPress Core

Opened 15 years ago

Closed 11 years ago

Last modified 8 months ago

#10657 closed feature request (maybelater)

Allow many-to-many relationship between posts and attachments

Reported by: morgansson's profile morgansson Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.8.4
Component: Media Keywords: needs-patch phase-3-media-triage
Focuses: Cc:


For example if you have uploaded an image to a post, and then you want to re-use it in another post, that image "post_parent" cell in the database will be the first "post_id" where it was added only and not both. I think it should be an array for many reasons, and because I don't see any problem if this changes this way.

I'm actually using this code to get the thumbnails for each post in the main page of my site.

function bdw_get_images($imagesize='large') {
    global $post;
    // Get the post ID
    $iPostID = $post->ID;
    $image = null; // Clear the value from the previous post
    // Get all attachments from the post ID with type image
    $attachments =& get_children(array('post_parent'=>$iPostID,'post_type'=>'attachment','post_mime_type'=>'image'));
    // If attachments are found...
    if ($attachments == TRUE) {
        // ... go through all of them
        foreach($attachments as $att) {
            // Get the image source for every image attachment found
            // Post_ID, Size ('thumbnail', 'medium', 'large', 'full' or an Array), Icon (true/false)
            $image = wp_get_attachment_image_src($att->ID,$imagesize,false);
            break; // Comment this line to get all images inside the post
    if (isset($image)) { 
		return $image[0]; 
	} else {
		return false;

The problem here is that the thumbnail is only showing on the first post where it was attached to.
I know there are other ways to do what I want, but I just think this is the most semantic and the agile way.

I hope you could implement the change in further WordPress versions because it might open a lot of new possibilities on posting.

Thanks for your time.

Change History (24)

#1 @morgansson
15 years ago

Also I forgot to say that it might be related to #10651

#2 @scribu
15 years ago

  • Keywords needs-patch added; post_parent post_type attachment array removed
  • Milestone changed from Unassigned to 2.9
  • Summary changed from Posts column "post_parent" should be an array when "post_type" is "attachment" to Allow many-to-many relationship between posts and attachments
  • Version set to 2.8.4

SQL doesn't have arrays. One column can contain a single value per row.

Of course, you can use serialized arrays (which are strings), but they aren't good, because you can't query individual values from them.

There is a GSOC project called Photopress that will likely allow an image to be attached to more than one post.

#3 @dj-wp
15 years ago

I totally agree that a many-to-many relationship between posts and attachments would be really useful, as I need to achieve such a task right now.

#4 @dj-wp
15 years ago

This would be possible perhaps by having a table that allows linking two different posts together?

#5 @ryan
15 years ago

  • Milestone changed from 2.9 to Future Release

#6 @ericmann
14 years ago

  • Cc eric@… added

#7 @scribu
14 years ago

In the meantime, I have released a plugin that allows many-to-many relationships between arbitrary post types, without needing any extra tables - it just uses the postmeta table. It's called Posts 2 Posts.

#8 @mikeschinkel
14 years ago

  • Cc mikeschinkel@… added

Related: #14513

#9 @scribu
13 years ago

Marked #19283 as dup.

#11 @dreamwhisper
12 years ago

  • Cc dreamwhisper added

#12 @SergeyBiryukov
12 years ago

Closed #20540 as a duplicate.

#13 @griffinjt
12 years ago

Any movement/thoughts on this for the future? I'd love to help contribute to getting this patch in core if there was some consensus on how to approach it.

#14 @emzo
12 years ago

  • Cc emzo added

#15 @aesqe
12 years ago

I've spent a lot of time thinking about this problem, and, in my File Gallery plugin, this is how I've decided to implement a solution:

  • each attachment still has only one parent
  • if you want to reuse an attachment - you should duplicate it, especially if you're running a multi-language site and wish to have different captions on images, depending on language
  • when an attachment is duplicated, it retains all the data from the original attachment, but the actual attachment file is not duplicated on the file system
  • each duplicate gets a new meta field: "_is_copy_of", which contains the ID of the original attachment
  • each "original" attachment also gets a new meta field: "_has_copies", which contains IDs of all of its copies
  • these two meta fields are there for one reason only: if you want to delete an "original" attachment, you'll get a warning, giving you a few options as what to actually do: "delete all attachments and the file", "delete just the original attachment data, don't touch the file", "cancel"
  • if you choose "delete just the original attachment data", the first copy of the "original" becomes the new "original"
  • by default, the copies are filtered out of the media library, but that can be turned off easily on the settings page

In over two years and after over 100 000 downloads, there haven't been any complaints about this approach, and I personally don't see any obvious downside to it. It may sound messy, but it wasn't that difficult to implement.

I'd be grateful for any comments and suggestions.

#16 @knutsp
12 years ago

  • Cc knut@… added

Excellent approach. I hope this will some day make it into core.

#17 @anonymized_7658014
12 years ago

  • Cc caspar@… added

Second the request for a core solution. Attachment pages are an essential (and often underestimated) feature for professional blogging and photo journalism. At the same time, a gallery consisting of already uploaded images is practically unusable, because the navigation just “breaks”.

Since attachments and galleries are a core feature, the solution for this issue should be in core also.
With the enhanced media uploader in 3.5 and its slick “Create a new gallery” button this topic might eventually gain a more attention, since more users might try to use galleries with non-attached images and stumble upon the inconsistent image navigation behavior.

#18 @helen
11 years ago

#23434 was marked as a duplicate.

#19 @Funkatronic
11 years ago

A post relationship table would make an ideal solution. Instead of using post-parent as a one to many relationship, you use a many to many relationship, which allows you to use an image in multiple galleries. Then you could use said galleries where ever you please, either as stand alone or embedded in a post.

#20 @nacin
11 years ago

I'm closing this as maybelater. We can bring it back if/when #14513 is brought back.

#21 @nacin
11 years ago

  • Milestone Future Release deleted
  • Resolution set to maybelater
  • Status changed from new to closed

#22 @tar.gz
11 years ago

  • Cc code@… added

#23 @buffler
11 years ago

  • Cc jeremy.buller@… added

#24 @joedolson
8 months ago

  • Keywords phase-3-media-triage added
Note: See TracTickets for help on using tickets.