• Hi all,

    I am developing an archive footer and I need to get the image with a post.

    This code works for title and text but not for the image src,

    $lastPosts = $wpdb->get_results("SELECT ID, post_title, post_content, meta_value FROM $wpdb->posts LEFT JOIN $wpdb->postmeta on wp_posts.ID = wp_postmeta.post_id WHERE post_status = 'publish' ORDER BY id DESC LIMIT 0,$i");
    if($lastPosts) {
    foreach($lastPosts as $lastPost) {
    $id = $lastPost->ID;
    $img = get_post_meta($id, '_wp_attached_file', 1);
    $link = get_permalink($lastPost->ID);
    etc.
    }}

    Any tips?

Viewing 2 replies - 1 through 2 (of 2 total)
  • You won’t be able to collect the _wp_attached_file for your posts, as one won’t exist. That particular meta data is assigned to ‘attachments.’

    WordPress adds a *special* record in the posts table for every attachment (file uploaded through the post editor), and the various custom fields assigned automatically (_wp_attached_file and _wp_attachment_metadata) belong to the attachment post record, not the current post you are creating and/or editing.

    You may want to look over my Post Image plugin for one method in collecting image attachments for a post:

    http://guff.szub.net/2006/02/09/post-image/

    Thread Starter DDT

    (@ddt)

    @kafkaesqui, tx for answering.

    I am going to dive into your sourcecode and look if I can understand it.
    In my case I am just looking for the first image thumbnail with each of my posts. If there’s no image I will display a default.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘How to get post image?’ is closed to new replies.