I have a query used to search projects based on default WordPress fields as well as meta values. I want to sort the results based on the meta key named
project_date, which is a timestamp. Here is the SQL I have so far.
SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta AS mt1 ON (wp_posts.id = mt1.post_id) WHERE 1 = 1 AND ((wp_posts.post_title LIKE '%testingsearch%') OR (mt1.meta_key = 'description' AND mt1.meta_value LIKE '%testingsearch%'))) AND wp_posts.post_type = 'project' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.id
I’m not sure how to include the meta field
project_date in an
ORDER BY. Obviously something like this won’t work since you can’t use a
WHERE in the
ORDER BY segment:
ORDER BY mt1.meta_value WHERE mt1.meta_key = 'project_date' DESC
How can I sort by this meta_key?
Read more here: Order WP_Query results by meta key value in custom query