从db获取2个指定月份之间的所有帖子

时间:2016-04-07 作者:yjs

An image illustration on how a drop down for news archive should be after php is added我试图在两个特定的日历月之间获得wp帖子。问题是,这是一年。所以这个月从五月开始,到明年四月结束。因此,每年的产量应该从年初的5月开始,到明年的4月。(例如:2015年5月至2016年4月)

我在一些插件的帮助下尝试了一下,但它只会在一个日历年(1-12月)左右输出帖子。

完成php后,我需要显示2015年5月至2016年4月的博客列表,从下拉列表中选择。。。

我的代码如下;

$args=array(
    \'orderby\' => \'date\',
    \'order\' => \'ASC\',
    \'posts_per_page\' => 1,
    \'caller_get_posts\'=>1
);
$oldestpost =  get_posts($args);

$args=array(
    \'orderby\' => \'date\',
    \'order\' => \'DESC\',
    \'posts_per_page\' => 1,
    \'caller_get_posts\'=>1
);
$newestpost =  get_posts($args);

if ( !empty($oldestpost) && !empty($newestpost) ) {
  $oldest = mysql2date("Y", $oldestpost[0]->post_date);
  $newest = mysql2date("Y", $newestpost[0]->post_date);
  $years = $newest - $oldest;

  echo \'<select onchange="if (this.value) window.location.href=this.value"><option value="" selected>Select Year</option>\';
  for ( $counter = 0; $counter <= $years; $counter += 1) {
    $endDate = $oldest+1;

    $where = apply_filters( \'getarchives_where\', "WHERE post_type = \'post\' AND post_status = \'publish\' AND  post_date BETWEEN \'$oldest-05-01\' AND \'$endDate-04-30\'" );

    $join = apply_filters( \'getarchives_join\', \'\' );

    if ( $months = $wpdb->get_results( "SELECT YEAR(post_date) AS year, MONTH(post_date) AS numMonth, DATE_FORMAT(post_date, \'%M\') AS month, count(ID) as post_count FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC" ) ) {

        foreach ( $months as $month ) {
            $currentYear = $month->year;
            if ( $prevYear !== $currentYear ) {
                echo \'<option value=\' . esc_url( get_year_link( $month->year ) ) .\'>\'. esc_html( ($currentYear).\'-\'.($currentYear+1) ) . \'</option>\';
            }
            $prevYear = $currentYear;
            if ( ( $currentYear !== $prevYear ) && ( \'\' !== $prevYear ) ) {
                echo \'</select>\';
            }
        }
    }
    $oldest++;
}
?>

<?php
echo \'</select>\';
}


**Elaborating this question with a list---Just an illustrative.**

*if 2006 selected the archive page will list post like below.
o May 2006
o June 2006
o July 2006
o August 2006
o September 2006
o October 2006
o November 2006
o December 2006
o January 2007
o February 2007
o March 2007
o April 2007

Like above all other year lists same...
* 2005
* 2004
* 2003
* 2002

3 个回复
SO网友:Vidhya Thirugnanam

将此添加到$args

\'date_query\' => array( array( \'after\' => \'January 1st, 2015\', \'before\' => array( \'year\' => 2016, \'month\' => 1, \'day\' => 1, ), \'inclusive\' => true, ), ),

SO网友:Bruno Cantuaria

Try those args:

\'date_query\' => array(
    //start date
    array(
        \'year\' => $startyear,
        \'month\' => $startmonth,
        \'compare\' => \'>=\'
    ),
    //end date
    array(
        \'year\' => $endyear,
        \'month\' => $endmonth,
        \'compare\' => \'<=\'
    )
),
SO网友:Owais Alam

WP\\u Query提供了一个date\\u查询参数,允许您使用before和after设置范围。

$args = array(
    \'date_query\' => array(
        array(
            \'after\'     => \'January 1st, 2015\',
            \'before\'    => \'December 31st, 2015\',
            \'inclusive\' => true,
        ),
    ),
);
$query = new WP_Query( $args );
为了进一步了解https://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters