Welcome to WebmasterWorld Guest from 18.104.22.168 , register , free tools , login , search , subscribe , help , library , announcements , recent posts , open posts Subscribe to WebmasterWorld
meta value has a date, how to use order by? indyank msg:4590515 12:04 pm on Jul 5, 2013 (gmt 0) I have a custom field called event_date (meta_key). Now value for this custom field are stored in "dd month year" format. Eg : 01 July 2013. How do I sort the wordpress query by this custom field? I have tried this in pre_get_posts filter fucntion, but it doesn't seem to work.. $query->set('meta_key' , 'event_date'); $query->set('orderby' , 'STR_TO_DATE( meta_value, "%d %M %Y" )'); I have found a few answers on the web where storing dates in yyyy/mm/dd format has worked with this. $query->set('meta_key' , 'event_date'); $query->set('orderby' , 'meta_value'); But in my case, the date is stored in a different format. meta_value is a column of longtext data type. Can someone pls. help.
lorax msg:4590527 12:44 pm on Jul 5, 2013 (gmt 0)
What version of MySQL and PHP are you running? indyank msg:4590549 1:34 pm on Jul 5, 2013 (gmt 0)
PHP - 5.4.9 Mysql - 5.5.31 But I think it is a wordpress limitation as I am trying to alter the main query in wordpress. lorax msg:4590578 3:42 pm on Jul 5, 2013 (gmt 0)
Off the top of my head I agree with you. Have you tried creating your own query to see if you can manipulate the result set? indyank msg:4590612 5:38 pm on Jul 5, 2013 (gmt 0)
Unfortunately, crating my own query wasn't an option. So the workaround was to somehow convert the existing meta-values for 'event_date' meta_keys into the ISO format "yyyy-mm-dd" and then this worked. $query->set('meta_key' , 'event_date'); $query->set('orderby' , 'meta_value'); Thanks lorax.