Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've queried all children of the post, and two records are shown. This is ok because there are 2 images in the gallery, but I can see no field in the database which makes it clear which one is the featured image.

I have joined the wp_postmeta table also, but there is no meta_key which indicates which of the 2 is the featured image.

Note: I do not want to look in the admin panel, I need a programmatic way to determine from the current database setup.

Does anyone know how the system knows which is the featured image in the db?

share|improve this question
3  
Look at how get_post_thumbnail_id() pulls data on post thumbnails, that should give you some clues.. –  t31os Aug 11 '11 at 16:26
2  
This is the solution - apparently there is a record in the postmeta table which registers the thumbnail ID against the post id, so post_id = 'XX' and meta_key is '_thumbnail_id' –  Ash Aug 11 '11 at 16:55
add comment

1 Answer

this comes down to a simple one line and there is no need to get all of the children using:

$featured image_id = get_post_meta( $post_id, '_thumbnail_id', true );
share|improve this answer
    
Why not just use get_post_thumbnail_id( $post->ID )? –  Chip Bennett Aug 11 '11 at 20:11
    
@Chip: You could, its basically a wrapper for the get_post_meta i posted with validation for the post id. If you look at the code here you will see that its the same. –  Bainternet Aug 11 '11 at 21:41
    
true, but I think it might be a bit easier to understand. :) In any case, +1 for the one-line-of-code solution! –  Chip Bennett Aug 12 '11 at 1:36
    
My question was asking where is this listed in the database. As such the first answer gave me the solution –  Ash Aug 16 '11 at 12:09
    
its listed in the postmeta table under meta_key named _thumbnail_id for each post –  Bainternet Aug 16 '11 at 14:05
add comment

Your Answer

 
discard

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.