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

We are building an Android app based on a Wordpress mysql database. We were able to get most of the information from the datase to display on to the app. Now all we have left is to retrieve the posts featured image.

Is there a way to have the featured image output a URL?


share|improve this question
up vote 6 down vote accepted

The featured image attachment id is stored in the post_meta table, with the meta_key _thumbnail_id.

Use that post id to find the post meta with meta_key _wp_attachment_metadata.

The value of that is a serialized array with keys thumb, file, width, height, sizes (an array), and image_meta (an array).

The file value is relative to the uploads directory for the WP install, and points to the original full-sized image. You should be able to calculate the URL from that.

sizes has an image size as its key (e.g., 'small', 'medium', 'large', 'thumbnail'), and the value is an array with keys file, height, and width.

image_meta will contain things like exif/iptc image metadata from the image itself.

share|improve this answer

The post's featured image is a regular old attachment to the post. So, as with any other image attachment, you'll need to query the wp_posts table for a post type of attachment. The trick is to first get the attachment ID that you need to grab.

This is contained in the given post's meta information, specifically the _thumbnail_id meta.

So, you'll need a query that:

  1. Retrieves the value marked by the key _thumbnail_id in the wp_postmeta table based on a given post ID.
  2. Retrieves the attachment from the wp_posts table based on the ID retrieved in step #1
  3. Generates an attachment permalink based on the information retrieved in step #2

Really, though, you're doing a lot of work to reinvent the wheel here ...

share|improve this answer

The post thumbnail is a stored in the {$wpdb->prefix}_postmeta table with the key _thumbnail_id.

Something like this:

SELECT * from {$wpdb->prefix}_posts 
   SELECT meta_value FROM {$wpdb->prefix}_postmeta 
   WHERE meta_key = '_thumbnail_id'
   AND post_id = ':ID'

Replace :ID with the post id.

That will return the Post associated with the image itself -- the post_title will be whatever the user associated with the title attribute on the image, and the guid will be the full sized image URL.

If you want different sizes, you'll have to look up the _wp_attachment_metadata postmeta value which will contain a serialized array with all the different sizes.

Using WordPress' api is much easier:

   $img = wp_get_attachment_img_src(get_post_thumbnail_id($some_post_id), 'the_image_size');
   // returns an array, the first item is the image URL
   echo $img[0];
share|improve this answer
Don't depend on the guid field to be a URL ... it's meant to be a unique identifier, not a link. – EAMann Jul 6 '12 at 21:14
That's a good point! Is there' a field that stores that actual image URL outside of the _wp_attachment_metadata postmeta field? – chrisguitarguy Jul 6 '12 at 21:15
That's what we are trying to figure out... I've found the actual image file on the database, but we need a way to call the image inside the sql query. – Martin Hasan Jul 6 '12 at 21:18
Take a look at wp_get_attachment_url() and how it builds the URL for attached images based on the ID ... – EAMann Jul 6 '12 at 21:27

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.