homepage Welcome to WebmasterWorld Guest from 54.196.199.118
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / WordPress
Forum Library, Charter, Moderators: lorax & rogerd

WordPress Forum

    
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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / WordPress
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved