0

An image illustration on how a drop down for news archive should be after php is addedAm 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
7
  • Look at the date_query parameter in WP_Query. Also note, caller_get_posts are long time depreciated and should not be used Commented Apr 7, 2016 at 12:58
  • @PieterGoosen: could you please give a hint on the above code?
    – yjs
    Commented Apr 7, 2016 at 12:59
  • @PieterGoosen: I reffered to date_query as you suggested...welcome. But If am using date_query, how could I know the start and end year of the posts? its coming from db, right?
    – yjs
    Commented Apr 7, 2016 at 13:05
  • @PieterGoosen: What change can i make in args section for year?
    – yjs
    Commented Apr 7, 2016 at 13:09
  • Can you please refer at once this link Commented Apr 7, 2016 at 13:17

3 Answers 3

0

Try those args:

'date_query' => array(
    //start date
    array(
        'year' => $startyear,
        'month' => $startmonth,
        'compare' => '>='
    ),
    //end date
    array(
        'year' => $endyear,
        'month' => $endmonth,
        'compare' => '<='
    )
),
4
  • And where from $startyear and $endyear come from? how? can you eloborate on that please?
    – yjs
    Commented Apr 7, 2016 at 13:26
  • You should set it, either manually or programatically, depending of what your solution should be. For the question you can just replace the startyear by 2015, startmonth to 5, endyear to 2016 and endmonth to 4 Commented Apr 7, 2016 at 18:51
  • But if the year is coming from db, how can i set year? month and day is ok...
    – yjs
    Commented Apr 8, 2016 at 4:08
  • Just like you did with the day and month Commented Apr 9, 2016 at 21:29
0

WP_Query offers a date_query parameter, allowing you to set a range with before and after.

$args = array(
    'date_query' => array(
        array(
            'after'     => 'January 1st, 2015',
            'before'    => 'December 31st, 2015',
            'inclusive' => true,
        ),
    ),
);
$query = new WP_Query( $args );

For further understanding https://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

0

Add this in your $args

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

1
  • Tried before, but am not trying this for one year,,,,am checking years from wp db...
    – yjs
    Commented Apr 7, 2016 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.