Make WordPress Core

Opened 9 years ago

Last modified 3 years ago

#30691 reopened defect (bug)

Removing a featured image does not remove the 'post_parent' value - reproducible

Reported by: juggledad's profile juggledad Owned by:
Milestone: Awaiting Review Priority: normal
Severity: major Version:
Component: Media Keywords: 2nd-opinion
Focuses: Cc:

Description

Removing a featured image does not remove the 'post_parent' value in the wp_posts row associated with the attachment. This causes invalid results when using functions like get_children() and get_posts().

1) install a fresh copy of WordPress 4.0.1
2) edit the first post and add a featured image
3) use phpMyAdmin and look at the wp_posts table. You will see an entry for the attachment and the 'post_parent' column will be set to the ID of the first post
4) Edit the first post and remove the featured image
5) again use phpMyAdmin and look at the wp_posts table. You will see the entry for the attachment and the 'post_parent' column WILL STILL BE SET to the ID of the first post.

to see a example of an error with get_children() do the following after doing the above.
1) activate twenty-thirteen and view the site
2) edit twenty-thirteen's 'content.php'. Line 15-19 should be

		<?php if ( has_post_thumbnail() && ! post_password_required() && ! is_attachment() ) : ?>
		<div class="entry-thumbnail">
			<?php the_post_thumbnail(); ?>
		</div>
		<?php endif; ?>

add the following right after it.

<?php
	$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID ); 
	$attachments = get_posts($args);
 	if (! empty($attachments)) {
 		echo 'attachment ID ='.$attachments[0]->ID.'<br>';
 		echo 'attachment post_parent ='.$attachments[0]->post_parent.'<br>';
	 	echo '$post->ID ='.$post->ID.'<br>';
    	echo '<img src="' . $attachments[0]->guid . '"/>';
		var_dump($attachments);
	}
?>

3) view the site and you will see the featured image.
4) use phpMyAdmin edit the entry for the attachment and set the 'post_parent' column to '0'.
5) view the site and you will NOT see the featured image.

Change History (15)

#1 @SergeyBiryukov
9 years ago

  • Component changed from General to Media

#2 @SergeyBiryukov
9 years ago

Related: #27983.

post_parent is used as an "Uploaded to" value, which just means that you had the post open when you uploaded the image. It describes original intent.

Removing the image from the post content or unsetting it as a featured image should not break the "Uploaded to" relationship, see comment:9:ticket:27983.

#3 follow-up: @juggledad
9 years ago

But if the post_parent value is not reset, using get_children() and get_posts() returns it even though it is no longer associated to that post.

Then this seems like a bug in those two functions. If the featured image is removed from a post and I use get_children(), it should not return that featured image since it is no longer associated with the post.

UPDATE: So i just tested adding a new image via media, then added a post and set the featured image to the new image I uploaded and in fact, the post_parent is set to '0' not the post's ID.

And when I remove the featured image, the featured image does not show and is not returned using the get_children() function.

So this begs the question, if an featured image is added while editing the post and subsequently removed, is the image still a child of the post? if so there are two different cases for posts and featured images...confusing....

Last edited 9 years ago by juggledad (previous) (diff)

#4 @knutsp
9 years ago

In some themes the faetured image is only show on archive pages or not at all. Still useful for setting an image to Open Graph. What if the featured image is still part of the post content when you remove it as featured image?

The only thing WordPress can safely detect is when an image is uloded to a specific post. Removing images may go undetected, even though removing the featured image as such may be detected.

#5 @juggledad
9 years ago

@knutsp - The main question is this: should get_children() and get_posts() return a link to a attachment when the attachment is no longer the featured image or an attachment in the post?

@SergeyBiryukov - from the codex 'get_children() retrieves attachments, revisions, or sub-pages, possibly by post parent.'
if the featured image is removed from a post, then isn't it is no longer an attachment? - yet it is returned as an attachment to the post.

Something is not consistent. If the image exists and I set it as a featured image then remove it, it IS NOT returned by get_children(). If I upload an image as the featured image then remove it, it IS returned by the get_children().

Last edited 9 years ago by juggledad (previous) (diff)

#6 in reply to: ↑ 3 @SergeyBiryukov
9 years ago

Replying to juggledad:

So this begs the question, if an featured image is added while editing the post and subsequently removed, is the image still a child of the post?

Technically yes, it's a different kind of relationship. It might be confusing, but post_parent just means the image was initially uploaded for this particular post (as opposed to uploading via Media Library screen), and has nothing to do with setting or removing featured image.

See #6820 for the ability to reattach an image to another post. There's also a plugin for that.

#7 @juggledad
9 years ago

'Technically yes, it's a different kind of relationship....'

How would one tell this relationship using get_children()?

#8 @juggledad
9 years ago

Any comments on what seems to be the main question:

Should get_children() and get_posts() return a link to a attachment when the attachment is no longer the featured image or an attachment in the post?

#9 @knutsp
9 years ago

Until the parent definition of a media attachment is completely redisigned or removed, yes this seems to be by design.

Such parent, as it is, means either uploaded to, once used as featured image for or was the first attachment for the post.

Practically, the parent of an attachment is not very useful to determine current usage of an attachment.

Either WordPress should aim to detect the addition and removal of an attachment from the post content, or the parent field should not be used at all. Setting the parent when uploded does not guarantee it's actually being used in that post. You can insert it, delete it and later use it in antoher post. It's still connected to the post it was first uploaded to.

The rule for a featured image just follows the same path.

Current internal rule seems to be: Set the parent whenever an attachment can be connected to a post, and never remove it later. Changing this for featured images and not for inline images doesn't make it more useful.

  • Stop assuming a value in the parent field of an attachment tells you anything about current state.
  • Start a general discussion on the use of the parent field for attachments. Maybe clarify documentation further?

#10 @knutsp
9 years ago

  • Keywords close added

See #6820 comment 31 by Ryan

We need a post2post table/API and an API to identify media used in a post. Then media can be "attached" to several posts, and when an image is removed, either as inline or featured, then this table can be updated.

Suggest "maybelater".

#11 @wonderboymusic
9 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to maybelater
  • Status changed from new to closed

As per comment:10

#12 follow-up: @junaidbhura
7 years ago

  • Keywords 2nd-opinion added; close removed
  • Resolution maybelater deleted
  • Severity changed from normal to major
  • Status changed from closed to reopened
  • Version 4.0.1 deleted

@knutsp I totally agree with you. There needs to be better management of media and attachments.

If an image is "removed" from a post, it should be considered as "deleted" from that post and stay in an unattached state automatically.

The function get_attached_media() would seem to suggest that it returns the media that are attached to a post "right now". So if an image is "removed" from a post, and if it still shows up as a result of get_attached_media() , it's obviously wrong since as far as a content editor is concerned, it is no longer part of that post.

Last edited 7 years ago by junaidbhura (previous) (diff)

#13 in reply to: ↑ 12 @SergeyBiryukov
7 years ago

  • Milestone set to Awaiting Review

Replying to junaidbhura:

If an image is "removed" from a post, it should be considered as "deleted" from that post and stay in an unattached state automatically.

There are backwards compatibility concerns though, see comment:9:ticket:27983.

Last edited 5 years ago by SergeyBiryukov (previous) (diff)

#14 @loboyle
7 years ago

There are other unfortunate consequences of this too, the status of the parent post can prevent featured images from embedding correctly when they're attached to other posts in the REST API - https://github.com/WP-API/WP-API/issues/2596

This ticket was mentioned in Slack in #core-media by antpb. View the logs.


3 years ago

Note: See TracTickets for help on using tickets.