I have a query that returns all posts from a DB table that match the given criteia, but I’m looking for a way to only return up to 5 posts from each 'post_type'. Currently the query is selecting every single post that matches and I am having to limit the numbers from each 'post_type' in PHP, which is not particularly efficient.
Can this be done? Thanks.
SELECT ID, post_title, post_type
FROM wp_posts
WHERE 1=1
AND post_status = "publish"
AND (
post_type IN (
"staff"
)
AND post_name LIKE "%The%"
)
OR (
post_type NOT IN (
"staff",
"Attachment"
)
AND (
post_name LIKE "%The%"
OR post_title LIKE "%The%"
)
)
ORDER BY post_type, post_name ASC
This solution will select the five most recent (based on
id) posts perpost_type: