I need to retreive a list of posts that have (at least) one attachment that belongs to a category in WordPress.
The relation between attachments and categories I made by myself using the WordPress default method.
Here’s the query that i’m running right now:
SELECT post.*
FROM `bma_posts` AS post
WHERE
EXISTS (
SELECT 1
FROM `bma_posts` AS attachment
JOIN `bma_term_relationships` AS relationship ON
relationship.`object_id` = attachment.`ID`
AND
relationship.`term_taxonomy_id` IN (17,15,16,5)
WHERE
attachment.`post_parent` = post.`ID`
AND
attachment.`post_type` = 'attachment'
)
AND
post.`post_type` = 'post'
AND
post.`post_status` = 'publish'
ORDER BY post.`post_date` DESC
LIMIT 3
The problem now is that I can’t get the attachment.ID
to know “who” included that post on the query.
1 comment