WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 15 months ago

Last modified 9 months ago

#10657 closed feature request (maybelater)

Allow many-to-many relationship between posts and attachments

Reported by: morgansson Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.8.4
Component: Media Keywords: needs-patch
Focuses: Cc:

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

comment:1 morgansson5 years ago

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

comment:2 scribu5 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.

comment:3 dj-wp5 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.

comment:4 dj-wp5 years ago

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

comment:5 ryan5 years ago

  • Milestone changed from 2.9 to Future Release

comment:6 ericmann4 years ago

  • Cc eric@… added

comment:7 scribu4 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.

comment:8 mikeschinkel4 years ago

  • Cc mikeschinkel@… added

Related: #14513

comment:9 scribu3 years ago

Marked #19283 as dup.

comment:11 dreamwhisper2 years ago

  • Cc dreamwhisper added

comment:12 SergeyBiryukov2 years ago

Closed #20540 as a duplicate.

comment:13 griffinjt2 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.

comment:14 emzo2 years ago

  • Cc emzo added

comment:15 aesqe2 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.

comment:16 knutsp2 years ago

  • Cc knut@… added

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

comment:17 glueckpress20 months 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.

comment:18 helen17 months ago

#23434 was marked as a duplicate.

comment:19 Funkatronic15 months 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.

comment:20 nacin15 months ago

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

comment:21 nacin15 months ago

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

comment:22 tar.gz15 months ago

  • Cc code@… added

comment:23 buffler9 months ago

  • Cc jeremy.buller@… added
Note: See TracTickets for help on using tickets.