BlueAirplane
Veteran
Just Getting Started
Member Likes (0)
I'm trying to find out how to display my posts by order of my custom field "event-date" (date picker), instead of date/time the post was published.
I basically need to list my custom events chronologically, from the closest date to furthest away.
Thanks!
Responses (10)
Developer (joined January 2012) Likes (0)
It would something like
`
query_posts('some_query&orderby=meta_value&meta_key=date_field&order=DESC);
That's rough as I don't have time to set it up and test. date_field would be the field ID assigned in Custom fields.
Note that the date_field's value is a string in the database so you need to pick a date format that sorts properly like yyyy-mm-dd.
Support Kangaroo (joined March 2011) Likes (0)
Greetings BlueAirplane,
We have not heard back from you as to the status of this issue.
If you are still having an issue please let us know so that we may try to get you fixed up as soon as possible by choosing to check mark this ticket as unresolved below and posting any new errors or symptoms you are noticing.
This action will also bring your ticket up front back in plain view again within the ticket system.
Thank you for being a WPMU Dev member!
Cheers, Joe
Member (joined December 2011) Likes (0)
Thank you Arnold & Joe,
Arnold, your tip worked and led me on the right path. Thank you. For other people trying to do something similar, this is the actual code I ended up using in my loop:
...whatever you want to display in your loop goes here.....
And just to clarify for others, 'event_listing' is my post type in CustomPress I wanted to query and '_ct_datepicker_4fbd4c15355e6' is the id of my custom datepicker field in CustomPress.
Thanks for the help!
Support Kangaroo (joined March 2011) Likes (0)
Greetings BlueAirplane,
Thank you for letting us know and for taking the time posting the code that enabled you to resolve your preferential issue.
Some rep points sent your way.
Cheers, Joe
Member (joined June 2011) Likes (0)
I have the same issue and BlueAirplane's code is on the right track but is flawed in that it orders the posts alphanumerically and therefore it will order them incorrectly.
eg. order ascending will put dates in the following order:
10 July, 2012
11 July, 2012
12 July, 2012
13 July, 2012
2 July, 2012
3 July, 2012
meta_key' => '_ct_datepicker_4fbd4c15355e6 needs to be converted into a unix time stamp before sorting but I have no idea how to do this.
Happy to pay for a solution if anyone knows how to code this for me.
Cheers
Chris
Member (joined June 2011) Likes (0)
*bump*
Anyone able to help with this or should I start a new discussion?
Developer (joined January 2012) Likes (0)
The easy way to do it is set the format for the date picker field to a sortable format like 2012-06-22. Then it would sort just fine.
Converting a string like 3 July, 2012 to a timestamp would use PHP strtotime() but that doesn't get it stored that way in the database so it doesn't help your sorting.
The date field in a custom field is just a string and has no dateness attached to it other than how you specify the format.
Support Chimp (joined March 2010) Likes (0)
Hey there, just checking in to see how things are going here as we have not heard from you in a short while.
If you need anything further then please feel free to reopen the thread or create a new one if needed.
Take care and have a great day!
Member (joined December 2012) Likes (0)
@BlueAirplane
Which template file did you put your code into? Did you create a custom archive file for your custom content type?
Support Chimp (joined March 2010) Likes (0)
It was probably done in the CPT file, so if you have a post type called "books" the file name would be:
single-books.php
See here for more details on template hierarchy:
http://codex.wordpress.org/Template_Hierarchy
You could copy the contents of your themes single.php as a basis for your changes.
Take care.
WordPress Questions?
We've got answers!
Find out more »