Opened 4 years ago

Closed 3 weeks ago

Last modified 3 weeks ago

#10657 closed feature request (maybelater)

Allow many-to-many relationship between posts and attachments

Reported by: morgansson Owned by:
Priority: normal Milestone:
Component: Media Version: 2.8.4
Severity: normal Keywords: needs-patch
Cc: eric@…, mikeschinkel@…, dreamwhisper, emzo, knut@…, caspar@…, code@…

Description

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 (22)

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

  • 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.

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.

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

comment:5   ryan3 years ago

  • Milestone changed from 2.9 to Future Release
  • Cc eric@… added

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.

  • Cc mikeschinkel@… added

Related: #14513

Marked #19283 as dup.

  • Cc dreamwhisper added

Closed #20540 as a duplicate.

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.

  • Cc emzo added

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.

  • Cc knut@… added

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

  • 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.

#23434 was marked as a duplicate.

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.

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

  • Milestone Future Release deleted
  • Resolution set to maybelater
  • Status changed from new to closed
  • Cc code@… added
Note: See TracTickets for help on using tickets.