![](/img/trans.png)
[英]How to get posts order by menu_order in WordPress using $wpdb->get_results()?
[英]WordPress get_results order by post date
我第一次在 WordPress 中使用“get_results”,需要按日期對結果進行排序。 到目前為止我所得到的是:
$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>';
}
?>
我們可以通過兩種方式做到這一點。
從你的代碼。
更改以下代碼
// 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; ?>
您應該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。
我想在某些日期之間獲取帖子,例如2009年10月24日至2019年11月1日。要獲取該帖子的查詢應使用什么查詢SQL數據庫。 我需要所有類別的帖子。 謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.