WordPress.org

Ready to get started?Download WordPress

Forums

Parent ID of image in wp_posts table not updated? (8 posts)

  1. haris.osmanagic
    Member
    Posted 1 year ago #

    Hi all,

    When I add a new image to the Media Library, its data gets inserted into wp_posts table, and that seems fine.

    When I use that image for the first time, in the sense that I insert it into a post, its parent ID gets updated to the post ID, and this seems fine as well.

    However, when I afterwards try to insert this same image into a new post, using Media Library (and not by uploading it again), its parent ID doesn't get updated!

    My question is: is this behavior expected?

    This effects using get_children() and get_posts() for the purpose of getting a list of all attached images.

    Thanks,

    Haris

  2. esmi
    Forum Moderator
    Posted 1 year ago #

    when I afterwards try to insert this same image into a new post, using Media Library (and not by uploading it again), its parent ID doesn't get updated!

    That's the correct (as in "expected") behaviour. If an image is not attached to a Post or Page when it is uploaded (by uploading through Add Media) or attached to a post manually, it is automatically attached to the first post that uses it. Once that attachment is set, it isn't updated.

  3. haris.osmanagic
    Member
    Posted 1 year ago #

    That was a pronto answer, thanks!

    So, having this in mind, there's not really any part of WP API I can use to get all images in a post? (One of the solutions is examining its content, but I wonder if there's any function maybe already doing it.)

  4. esmi
    Forum Moderator
    Posted 1 year ago #

    You can currently only get all images attached to a given Post or Page. As you implied, the content of each Post/Page could be parsed looking for img tags but, as this is going to be very intensive (and therefore have performance implications) I don't see it being added to WordPress core any time soon.

  5. haris.osmanagic
    Member
    Posted 1 year ago #

    Again, thanks.(Y)

    How much sense would it make for WP to:

    a) store a list of parent IDs for an image, or:
    b) store a new record each time an image is inserted?

  6. esmi
    Forum Moderator
    Posted 1 year ago #

    It might make sense for you but it's not a request I've seen that often (if at all). Because of WP's market, it's only the really popular feature requests that get added to core. anything less popular is deemed to be plugin territory.

    You could have a look around and see if there's a plugin out there that does this for you. There's also nothing to stop you pitching this on http://wordpress.org/extend/ideas/ - if it hasn't already been suggested.

  7. haris.osmanagic
    Member
    Posted 1 year ago #

    That's exactly and everything I wanted to know about this at the moment.

    Thanks a lot for the great and quick answers, thumbs up!

  8. esmi
    Forum Moderator
    Posted 1 year ago #

    Glad I could help :-)

Topic Closed

This topic has been closed to new replies.

About this Topic