Forums

[resolved] ASC and DESC in SQL query not producing different results (3 posts)

  1. fgshepard
    Member
    Posted 2 years ago #

    I have a custom query that works but I want to reverse the order. For some reason, changing ASC to DESC doesn't seem to make a difference. Here is the SQL:

    SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id
    AND wpostmeta.meta_key = 'Date'
    AND wposts.post_status = 'publish'
    AND wposts.post_type = 'post'
    AND wposts.post_date < NOW()
    ORDER BY MID(wpostmeta.meta_value,7,10), MID(wpostmeta.meta_value,1,2), MID(wpostmeta.meta_value,4,5) DESC

    The custom field contains date in mm/dd/yyyy format, and I'm sorting the results initially by using the MID function on the value (so it sorts by year, then month, then day). The results come out in order from oldest to newest. Swapping ASC and DESC doesn't change the order. Any/All help much appreciated.

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    Wonder if this works:

    ORDER BY MID(wpostmeta.meta_value,7,10) DESC, MID(wpostmeta.meta_value,1,2) DESC, MID(wpostmeta.meta_value,4,5) DESC
  3. fgshepard
    Member
    Posted 2 years ago #

    Yes, that did it. Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic