In wordpress How can we fetch records in specific order within meta field value for eg I need to fetch all records where zip_code(meta key name) is all fields with 60007 first than all 60143 than 60191
Table drrr_posts
Id | post_name | content
2
3
4
22
32
43
Table drrr_postmeta
id | meta_key | meta_value | post_id
1 | zip_code | 60143 | 2
2 | zip_code | 60007 | 3
3 | zip_code | 60191 | 4
4 | zip_code | 60143 | 22
5 | zip_code | 60007 | 32
6 | zip_code | 60143 | 43
I think issue is I am passing ORDER BY FIELD( drrr_postmeta.meta_value not zip_code which is meta_key In mysql I can easily pass the values in ORDERBY like
ORDER BY FIELD(name, 'B', 'A', 'D', 'E', 'C')
here is what I have now My Query is
SELECT SQL_CALC_FOUND_ROWS drrr_posts.ID
FROM drrr_posts
INNER JOIN drrr_term_relationships ON ( drrr_posts.ID = drrr_term_relationships.object_id )
INNER JOIN drrr_postmeta ON ( drrr_posts.ID = drrr_postmeta.post_id )
INNER JOIN drrr_postmeta AS mt1 ON ( drrr_posts.ID = mt1.post_id )
WHERE 1 =1
AND drrr_posts.ID NOT
IN ( 91, 89, 87, 66 )
AND (
drrr_term_relationships.term_taxonomy_id
IN ( 11 )
)
AND drrr_posts.post_type = 'doctors'
AND (
(
drrr_posts.post_status = 'publish'
)
)
AND (
drrr_postmeta.meta_key = 'zip_code'
AND (
mt1.meta_key = 'zip_code'
AND CAST( mt1.meta_value AS SIGNED )
IN (
'60007', '60143', '60191', '60005', '60106', '60157', '60173', '60008', '60056', '60172'
)
)
)
GROUP BY drrr_posts.ID
ORDER BY FIELD( drrr_postmeta.meta_value, '60007', '60143', '60191', '60005', '60106', '60157', '60173', '60008', '60056', '60172' )
LIMIT 0 , 8