Skip to content

Instantly share code, notes, and snippets.

@alanedwardes
Created November 26, 2011 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanedwardes/1396447 to your computer and use it in GitHub Desktop.
Save alanedwardes/1396447 to your computer and use it in GitHub Desktop.
Evil bit of SQL to get the most recent posts from WordPress with media, the link to the media and the post ID.
SELECT posts.post_title, posts.ID post_id, media.ID media_id
FROM $wpdb->posts media
JOIN $wpdb->posts posts
ON posts.post_content LIKE CONCAT('%', media.guid, '%')
WHERE media.post_type = 'attachment'
AND posts.post_type = 'post'
AND posts.post_status = 'publish'
GROUP BY posts.ID
ORDER BY media.post_date DESC
LIMIT 0, 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment