Am trying to get wp posts between two specific calendar month. The problem is, it is a annual year. So month starts from May and ends in Next year April. So each year output should start from May of starting year to April of next year. (Eg: May 2015 - Apr 2016)
I tried out myself with help of some plugins, but it will output only posts around a calendar year (jan-dec).
After complete php is done, I need to show blog listing from May 2015 to April 2016 when selected from drop down...
My code is below;
$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
date_query
parameter inWP_Query
. Also note,caller_get_posts
are long time depreciated and should not be used