WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using this code to get the url of first inserted image in a post. Is there any way I can get the ID of that image.

      global $post, $posts;
      $first_img = '';
      $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
      $first_image = $matches [1] [0];
share|improve this question
on default image should have a class with attachment id like attachment-{id}. Its the only way i can think of. – Sisir Aug 2 '12 at 18:25
up vote 0 down vote accepted

No, an image placed inside a post/page content don't reference in your database other than inside your content only.

That's why an image attached is better: you have full control over your images since they are in your server and referenced in your database. An attachment is a child post, once you attach an file to a post/page you can use get_children for retrieve file description, title etc.

share|improve this answer
Thanks .... by the way to whoever down-voted it would be helpful to maybe mention why? – byronyasgur Aug 2 '12 at 20:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.