您还必须在第二个SQL查询中定义post顺序。试试这个。
// get years that have posts
$years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM wp_posts WHERE post_type = \'post\' AND post_status = \'publish\' GROUP BY year DESC" );
foreach ( $years as $year ) {
// get posts for each year
$posts_this_year = $wpdb->get_results( "SELECT * FROM wp_posts WHERE post_type = \'post\' AND post_status = \'publish\' AND YEAR(post_date) = \'" . $year->year . "\' ORDER BY post_date DESC" );
echo \'<h2>\' . $year->year . \'</h2><ul>\';
foreach ( $posts_this_year as $post ) {
echo \'<li>\' . $post->post_title . \'</li>\';
}
echo \'</ul>\';
}