Title: patch for posts displaying in reverse order
Last modified: August 19, 2016

---

# patch for posts displaying in reverse order

 *  [mpelzsherman](https://wordpress.org/support/users/mpelzsherman/)
 * (@mpelzsherman)
 * [18 years, 2 months ago](https://wordpress.org/support/topic/patch-for-posts-displaying-in-reverse-order/)
 * OK, I’ve found a workaround for this issue for folks like me who are running 
   older versions of WordPress, have had problems upgrading, and whose hosting providers
   won’t upgrade to a non-GA release of MySQL to fix the problem.
 * Note: This isn’t 100% guaranteed not to cause problems. It’s working for me but
   YMMV.
 * In wp-includes/classes.php, look for this line:
 *     ```
       $request = " SELECT $distinct * FROM $wpdb->posts $join WHERE 1=1" . $where . " GROUP BY " . $groupby . " ORDER BY " . $orderby . " $limits";
       ```
   
 * Change it to this:
 *     ```
       if (trim($groupby)=="$wpdb->posts.ID") {
       			$request = " SELECT $distinct * FROM $wpdb->posts $join WHERE 1=1" . $where . " ORDER BY " . $orderby . " $limits";
       		} else {
       			$request = " SELECT $distinct * FROM $wpdb->posts $join WHERE 1=1" . $where . " GROUP BY " . $groupby . " ORDER BY " . $orderby . " $limits";
       		}
       ```
   
 * In other words, if we’re only doing a GROUP BY on the primary key of the posts
   table, there’s no need for the GROUP BY, so just take it out.
 * This will cause problems with the query that supports the “previous posts” link,
   so make this change in template-functions-links.php:
 * In the “posts_nav_link” function, change this line:
 *     ```
       preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches);
       ```
   
 * … to this:
 *     ```
       preg_match('#FROM\s(.*)\sORDER BY#siU', $request, $matches);
       ```
   
 * This is working fine for me so far. Hope it helps someone.

Viewing 1 replies (of 1 total)

 *  [kareobi](https://wordpress.org/support/users/kareobi/)
 * (@kareobi)
 * [18 years ago](https://wordpress.org/support/topic/patch-for-posts-displaying-in-reverse-order/#post-737363)
 * Hi mpelzsherman,
 * This works GREAT!!! THANK YOU!!
 * However the 2nd part didn’t work for me…
    >>”Cause problems with the query that
   supports the “previous posts” link,”
 * It shows:
 * “WordPress database error: [You have an error in your SQL syntax; check the manual
   that corresponds to your MySQL server version for the right syntax to use near”
   at line 1]
    SELECT COUNT(DISTINCT ID) FROM”
 * No matter if I modify or not modify to
 * preg_match(‘#FROM\s(.*)\sORDER BY#siU’, $request, $matches);
 * Karen

Viewing 1 replies (of 1 total)

The topic ‘patch for posts displaying in reverse order’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [kareobi](https://wordpress.org/support/users/kareobi/)
 * Last activity: [18 years ago](https://wordpress.org/support/topic/patch-for-posts-displaying-in-reverse-order/#post-737363)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
