我正在使用" get_results"在WordPress中第一次需要按日期排序结果。到目前为止我得到的是:
$wpdb->get_results( "SELECT * FROM asi_posts WHERE post_type = 'post' AND post_status = 'publish' AND YEAR(post_date) = '" . $year->year . "'" );
这可以很好地检索帖子,虽然它显示的是列表顶部最旧的帖子,而不是最新的。
我应该在该查询中使用正确的语法来颠倒顺序吗?
感谢。
如果它更有用,请输入完整的代码:
<?php
// Get years that have posts
$years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM asi_posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY year DESC" );
// For each year, do the following
foreach ( $years as $year ) {
// Get all posts for the year
$posts_this_year = $wpdb->get_results( "SELECT * FROM asi_posts WHERE post_type = 'post' AND post_status = 'publish' AND YEAR(post_date) = '" . $year->year . "'" );
// Display the year as a header
echo '<div class="year-header-block">';
echo '<button class="btn-year-reveal">View</button>';
echo '<h3>' . $year->year . ' Industry News & Comment</h3>';
echo '</div>';
// Start an unorder list
echo '<ul class="no-list yearly-archive-list">';
// For each post for that year, do the following
foreach ( $posts_this_year as $post ) {
// Display the title as a hyperlinked list item
echo '<li><a href="' . get_permalink($post->ID) . '"><span class="news-title">' . $post->post_title . '</span><span class="news-date">' . get_the_time('F j, Y', $post->ID) . '</span></a></li>';
}
// End the unordered list
echo '</ul>';
}
?>
答案 0 :(得分:3)
我们可以通过两种方式实现。
来自您的代码。
更改以下代码
// Get all posts for the year
$posts_this_year = $wpdb->get_results( "SELECT * FROM asi_posts WHERE post_type = 'post' AND post_status = 'publish' AND YEAR(post_date) = '" . $year->year . "'" );
以下代码。
// Get all posts for the year
$posts_this_year = $wpdb->get_results( "SELECT * FROM asi_posts WHERE
post_type = 'post' AND post_status = 'publish' AND
YEAR(post_date) = '" . $year->year . "' ORDER BY post_date DESC" );
其他解决方案:
在主题的function.php文件中创建一个新功能 posts_by_year 。
function posts_by_year() {
// array to use for results
$years = array();
// get posts from WP
$posts = get_posts(array(
'numberposts' => -1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
));
// loop through posts, populating $years arrays
foreach($posts as $post) {
$years[date('Y', strtotime($post->post_date))][] = $post;
}
// reverse sort by year
krsort($years);
return $years;
}
并在模板中调用以下代码。
<?php foreach(posts_by_year() as $year => $posts) : ?>
<h2><?php echo $year; ?></h2>
<ul>
<?php foreach($posts as $post) : setup_postdata($post); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
答案 1 :(得分:1)
您应该ORDER BY xxx ASC
而不是DESC
获取最早的第一个,如下所示:
$years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM asi_posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY post_date ASC" );
此外,如果您想订购其他查询,它应如下所示:
$wpdb->get_results( "SELECT * FROM asi_posts WHERE post_type = 'post' AND post_status = 'publish' AND YEAR(post_date) = '" . $year->year . "' ORDER BY post_date ASC" );
注意:默认情况下,SQL总是按顺序排序,因此您可以删除指令ASC。但是,如果您希望它先订购旧版,则将ASC替换为DESC。