Selecting posts from wordpress db with thumbnail or intro images

I have been trying to fetch posts from a wordpress site’s mysql db to my own script for a few days.

<?php

 $querystr = "
    SELECT DISTINCT $wpdb->posts.* 
    FROM $wpdb->posts, $wpdb->postmeta
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
    AND $wpdb->postmeta.meta_key = 'tag' 
    AND $wpdb->postmeta.meta_value = 'email' 
    AND $wpdb->posts.post_status = 'publish' 
    AND $wpdb->posts.post_type = 'post'
    AND $wpdb->posts.post_date < NOW()
    ORDER BY $wpdb->posts.post_date DESC
 ";

 $pageposts = $wpdb->get_results($querystr, OBJECT);

 ?>

I can see thumbnail images on the original site but I could not figure out where to get that image location data in the db.

Read More

What would be the enhancements on my query in order to have image information with current ones?

Related posts

Leave a Reply

2 comments

  1. You need to dive into your wp_postmeta table. This is where you will find everything. You can get the thumbnail ID with the following query:

    SELECT * FROM 'wp_postmeta' WHERE post_id=**ID_HERE** AND meta_key='_thumbnail_id'
    

    You will get a thumbnail ID from which you will be able to get your real thumbnail with the following query:

    SELECT * FROM 'wp_postmeta' WHERE post_id=**PREVIOUSLY_OBTAINED_ID**
    

    This will actually give you two rows :

    1. One for the URL of your image (meta_key="_wp_attached_file")
    2. One for the image info like its dimensions, its title etc… (meta_key="_wp_attachment_metadata")