WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Joining postmeta and posts table to get image URLs (2 posts)

  1. maerk
    Member
    Posted 3 years ago #

    I'm trying to get the image url and ID for images attached to a particular post from the post meta table, joining onto the posts table using the Query below.

    The query as it stands returns an empty set. If I remove the WHERE posts.post_parent = $intPostId clause, it returns all the image attachments, which is nearly what I want -- but too much!

    What change do I need to make to the query to get the data I want?

    // $intPostID contains the post ID of the post where all the images are attached.
    
    SELECT
    	meta.post_id AS image_id, meta.meta_value AS url
    FROM
    	$wpdb->posts AS posts LEFT JOIN $wpdb->postmeta AS meta
    ON
    	posts.ID = meta.post_id
    WHERE
    	posts.post_parent = $intPostId
    AND
    	meta.meta_key = '_wp_attached_file'
    ORDER BY
    	posts.post_date DESC
  2. maerk
    Member
    Posted 3 years ago #

    So sorry!! Turns out I had the wrong value in $intPostId. This query works just fine!

Topic Closed

This topic has been closed to new replies.

About this Topic