3

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?

2
  • 3
    Look at how get_post_thumbnail_id() pulls data on post thumbnails, that should give you some clues..
    – t31os
    Commented Aug 11, 2011 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'
    – Mazatec
    Commented Aug 11, 2011 at 16:55

1 Answer 1

5

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 );
5
  • Why not just use get_post_thumbnail_id( $post->ID )? Commented Aug 11, 2011 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
    Commented Aug 11, 2011 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! Commented Aug 12, 2011 at 1:36
  • My question was asking where is this listed in the database. As such the first answer gave me the solution
    – Mazatec
    Commented Aug 16, 2011 at 12:09
  • its listed in the postmeta table under meta_key named _thumbnail_id for each post
    – Bainternet
    Commented Aug 16, 2011 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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