• Resolved maerk

    (@maerk)


    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
Viewing 1 replies (of 1 total)
  • Thread Starter maerk

    (@maerk)

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

Viewing 1 replies (of 1 total)
  • The topic ‘Joining postmeta and posts table to get image URLs’ is closed to new replies.