WordPress.org

Ready to get started?Download WordPress

Forums

How to sort posts by custom field date? (6 posts)

  1. e-win.gr
    Member
    Posted 1 year ago #

    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!

  2. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  3. e-win.gr
    Member
    Posted 1 year ago #

    So that i want cannot be done?

  4. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  5. e-win.gr
    Member
    Posted 1 year ago #

    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?

  6. vtxyzzy
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic