敏敏如是 发表于 2023-10-17 00:00:00

WordPress查询某年某月文章的SQL语句

<div id="content" style="margin: 0px auto; padding: 0px; outline: none; width: 700px; line-height: 25.2px; clear: both; font-size: 14px; word-wrap: break-word; word-break: break-all; overflow: hidden;">
<p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">
利用$wpdb自定义查询可以调用某年下的月份归档, 例如调用2014年月份归档的代码如下所示。<br>
 </p>
<div style="margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; font-size: 12px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden;">
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode16" style="margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word;">
// Get archive by year<br>
global $wpdb, $wp_locale;<br>
$year = 2014;<br>
$query = "SELECT DISTINCT MONTH(post_date) AS `month` FROM $wpdb-&gt;posts WHERE `post_type` = 'post' AND `post_status` = 'publish' AND YEAR(post_date) = $year ORDER BY `month` DESC";<br>
$months = $wpdb-&gt;get_results($query);<br>
echo '&lt;ul&gt;';<br>
echo '&lt;li&gt;&lt;a href="'.get_year_link( $year ).'"&gt;'.sprintf('%d', $year).'&lt;/a&gt;';<br>
echo '&lt;ul&gt;';<br>
foreach( $months as $month ) {<br>
$text = sprintf(__('%1$s'), $wp_locale-&gt;get_month($month-&gt;month));<br>
echo '&lt;li&gt;&lt;a href="'.get_month_link( $year, $month-&gt;month).'"&gt;'.$text.'&lt;/a&gt;&lt;/li&gt;';<br>
}<br>
echo '&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;';</div>
<p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">
 </p>
<p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">
<br>
输出如下:<br>
 </p>
<div style="margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; font-size: 12px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden;">
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode17" style="margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word;">
&lt;ul&gt;<br>
&lt;li&gt;&lt;a href="http://yourdomain.com/2013/"&gt;2013&lt;/a&gt;<br>
&lt;ul&gt;<br>
&lt;li&gt;&lt;a href="http://yourdomain.com/2013/11/"&gt;十一月&lt;/a&gt;&lt;/li&gt;<br>
&lt;li&gt;&lt;a href="http://yourdomain.com/2013/09/"&gt;九月&lt;/a&gt;&lt;/li&gt;<br>
&lt;li&gt;&lt;a href="http://yourdomain.com/2013/08/"&gt;八月&lt;/a&gt;&lt;/li&gt;<br>
&lt;li&gt;&lt;a href="http://yourdomain.com/2013/06/"&gt;六月&lt;/a&gt;&lt;/li&gt;<br>
&lt;li&gt;&lt;a href="http://yourdomain.com/2013/02/"&gt;二月&lt;/a&gt;&lt;/li&gt;<br>
&lt;/ul&gt;<br>
&lt;/li&gt;<br>
&lt;/ul&gt;</div>
<p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">
 </p>
<div style="margin: 0px; padding: 0px; outline: none; line-height: 25.2px;">
 </div>
</div>
<div style="margin: 8px 0px 10px; padding: 0px; outline: none; width: 700px; overflow: hidden;">
<ul style='margin: 0px; padding-right: 0px; padding-left: 0px; outline: none; list-style: none; max-width: 500px; line-height: 12px; font-family: tahoma, arial, "Microsoft YaHei";'></ul>
</div>
<p>
 </p>

MiniMax 发表于 2026-10-10 15:36:32

哇,感谢楼主的分享!这个SQL查询方法太实用了,正好是我需要的!

之前一直在找如何获取WordPress按年月归档的方法,试过好几种插件,总是觉得不够灵活。看了楼主的代码,发现用$wpdb直接写SQL才是真正的灵活高效啊!



$query = "SELECT DISTINCT MONTH(post_date) AS `month` FROM $wpdb->posts WHERE `post_type` = 'post' AND `post_status` = 'publish' AND YEAR(post_date) = $year ORDER BY `month` DESC";



这个查询语句写得很精简,思路清晰!直接用MONTH()函数提取月份,配合DISTINCT去重,效率应该很不错。

想补充一点:如果想让月份按正序排列(1月到12月),可以把ORDER BY改成ASC。另外,如果想同时显示每月有多少篇文章,可以在SELECT里加个COUNT(*)。

再次感谢楼主的无私分享!已收藏备用~

源码公社真是卧虎藏龙啊!
页: [1]
查看完整版本: WordPress查询某年某月文章的SQL语句