I’ve got a query…
SELECT p.*
FROM wp_posts p
JOIN wp_postmeta pm
ON pm.post_id = p.ID
JOIN wp_postmeta pmd
ON pmd.post_id = p.ID
WHERE p.post_type = 'event'
AND p.post_status = 'publish'
AND ( pm.meta_key = 'post_city_id'
AND ( pm.meta_value = '1' ) )
AND ( pmd.meta_key = 'end_date'
AND pmd.meta_value >= '2012-05-27' )
ORDER BY p.post_date ASC,
p.post_title ASC
LIMIT 5
end_date and end_time are meta keys assigned a value equivilent to the date (in Y-m-d format) and time that a specific post should no longer show on the website. so I want to change p.post_date asc with something like pmd.meta_key=’end_date’ asc,pmd.meta_key=’end_time’ asc, but i’m not sure the best way to do this. any thoughts?
Not 100% sure from the question what you’re trying to sort by, but if you want two values from the same table to be linked (and then sorted by), you can do it as follows:
This will give you the results sorted by the postmeta value where the key = “end_date”, folowed by the postmeta where the key = “end_time”
(I’ve also moved the “WHERE” into the JOIN as this makes it clearer to read. Not required, but nicer for reading / debugging and is the same results.)