Support » Fixing WordPress » How to sort posts by custom field date?

  • I want to sort my posts in a page by custom field date.

    For example:

    I have some posts (events) that have custom field (expiration date) and i want to sort them by the more close date.

    e.x.
    Post: Party Custom Field: 12/5/2013
    Post: Bachelor Party Custom Field: 10/4/2013
    Post: Kids Party Custom Field: 13/4/2013

    So i want to sort them in a page with the most close date first as below:

    Post: Bachelor Party Custom Field: 10/4/2013
    Post: Kids Party Custom Field: 13/4/2013
    Post: Party Custom Field: 12/5/2013

    Please help me!

    P.S. Also i want to make this for custom field (expiration hour)! I use the Advanced Custom Fields Plugin!

Viewing 5 replies - 1 through 5 (of 5 total)
  • You cannot sort on a date in the format you show. Dates must be in ‘YYYY/MM/DD’ (or similar) format to be sortable. So, instead of ’10/4/2013′, you would have ‘2013/10/04’.

    The easiest solution is to use the YYYY/MM/DD format for your dates and add the ‘meta_key’ => ‘YourCustomFieldName’ and ‘orderby’ => ‘meta_value’ arguments to the query.

    Otherwise, you will need to add filter functions to the query to rearrange the date so it can be sorted.

    So that i want cannot be done?

    The easiest solution is to use the YYYY/MM/DD format for your dates and add the ‘meta_key’ => ‘YourCustomFieldName’ and ‘orderby’ => ‘meta_value’ arguments to the query.

    It’s the easiest way but it doesn’t seem good in the view of the date format. Is there any other way to use the YYYY/MM/DD format and display it differently?

    You could create a second custom field to use for sorting and put the date in YYYY/MM/DD format there.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to sort posts by custom field date?’ is closed to new replies.