年度档案升序

时间:2014-09-02 作者:descentropy

嘿,我无意中发现this question 但我似乎无法联系作者,因为我在这里还是太新了。

我正在我的archives.php 页面显示按年份排序的我的帖子。它工作得很好,现在我想知道如何对帖子进行排序descending 排序,以便最新的帖子显示在顶部。

// 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 post_title FROM wp_posts WHERE post_type = \'post\' AND post_status = \'publish\' AND YEAR(post_date) = \'" . $year->year . "\'" );

    echo \'<h2>\' . $year->year . \'</h2><ul>\';
    foreach ( $posts_this_year as $post ) {
        echo \'<li>\' . $post->post_title . \'</li>\';
    }
    echo \'</ul>\';
}
这就是目前的情况:

image

非常感谢您的帮助。谢谢你来自柏林。

1 个回复
最合适的回答,由SO网友:Robert hue 整理而成

您还必须在第二个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>\';

}

结束

相关推荐

Hook into $wpdb

有没有办法连接到WPDB实例($WPDB)?我基本上想要的是在执行所需的查询get之前执行自己的函数。这可能吗?第一个答案的附加评论问题: $query = $wpdb->prepare(\"SELECT * FROM \" . $wpdb->prefix . \"test WHERE type = %s and ip = %s\", $type, $ip_full_log); $fetch = $wpdb->get_results($query, ARRAY_A)