Support » Everything else WordPress » Relations between images, posts and database

  • Hello,

    I have this problem since I have to use a query to get the attachments of a specific post.
    So the query like:

    $args = array(
    	'post_type' => 'attachment',
    	'post_mime_type' => 'image',
    	'numberposts' => -1,
    	'post_status' => 'inherit',
    	'post_parent' => $post->ID
    $attachments = get_posts($args);
    bla bla bla...

    but… if I delete from my article an image, or if I delete the thumb article also, anyway that images are returned if I use that query.
    So to see what’s happened, I went inside the database in the post table and I saw that the record did’t update.
    Moreover, if I use the same image in more than one article, a new record is not add. So i think that for an image there’s is only one post_parent, even if used in more than one place.

    Is it correct?
    thanks in advanced for support

Viewing 2 replies - 1 through 2 (of 2 total)
  • esmi


    Forum Moderator

    Images can only be attached to one Post or Page – usually the Post/Page that was being edited when the image was uploaded or the first Post/Page it was added to. This is irrespective of how many Posts or Pages you insert the image into. Also, if you remove an image from the content of it’s parent Post/Page, the attachment is not broken. So only one attachment record is ever added to the database per image.

    yes, that’s what I discover, and to be honest I think is a non sense.
    But anyway, make the query I wrote above returned wrong data, right?

    Better to do something like this in the loop:

    preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    $imgs = $matches[1];

    this gives the url of the images (use $matches[0]) to get the html of the images.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Relations between images, posts and database’ is closed to new replies.