0

I've managed to loop through the current month's posts in January but I am trying to get the previous month's posts in December of the previous year. I think this is where the problem might be, that it is 2014 and the the posts I am after is in 2013 (Dec). I'd appreciate it if someone can spot the problem. I have been through hordes of examples, the codex and the WordPress forums but not found anything that would work.

        <!-- PREVIOUS MONTH NEWS  -->

        <?php 

            $prevMonth = date('M', mktime(0, 0, 0, date('m'), 0, date('Y')));                                               

            $newsPrevArgs = array(
                'cat'               => 'news',
                'monthnum'          => $prevMonth,
                'order'             => 'DES',
                'posts_per_page'    => 5
            );
        ?>

        <div class="clearfix"></div>

        <h3 class="news-previous-month"><?php echo date('M Y', mktime(0, 0, 0, date('m'), 0, date('Y'))) ?></h3>

        <?php 

            $previous_news = new WP_Query( $newsPrevArgs );
            $isFirstNewsLoop = true;
            $newsItemNum = 0;

            // Loop through all news items

            while ( $previous_news->have_posts() ) {
                $previous_news->the_post(); ?>

                    <?php 
                        echo '<div class="news-article news-article-' . $newsItemNum . ' ' . ( $isFirstNewsLoop ? 'news-article-current' : '') . '">';


                            echo '<span class="news-date">' . get_the_date('d M Y', '<strong>', '</strong>') . '</span>';
                            echo '<span class="news-thumb">' . get_the_post_thumbnail($page->ID, 'home-widget-210') . '</span>';
                            echo '<span class="news-title"><strong>' . get_the_title() . '</strong></span>';
                            echo '<span class="news-blurb">' . get_the_excerpt() . '</span>';
                            wp_reset_postdata();
                        echo '</div>'
                    ?>


                <?php

                    $newsItemNum +=1;
                    $isFirstNewsLoop = false;


                ?>

        <?php } ?>
0

This could be a simple PHP issue of how you are asking to return the first date()

date('M', mktime(0, 0, 0, date('m'), 0, date('Y')));

is returning Dec, and WP_Query needs the month number. First, try changing 'M' to 'm'.

date('m', mktime(0, 0, 0, date('m'), 0, date('Y')));

If that doesn't work, try using the date_query and pass the year in as well.

Good luck!

|improve this answer|||||
0

You will need to use year in conjunction of monthnum.

// Calculate month or year based on your WordPress timezone setting
// As post date is based on the timezone you set on general setting page on wp-admin
$current_month_num = date('m', current_time('timestamp') );
$current_month_year = date('Y', current_time('timestamp') );

// if current month number is less that 2, what's probably 1|January
// then the last month will be 12|December
// and year will be last year
if( $current_month_num < 2 )
{
    $prev_month_num = 12;
    $prev_month_year = $current_month_year - 1;
}
// if month number is not less that 2, then previous month will be from current year
else
{
    $prev_month_num = $current_month_num - 1;
    $prev_month_year = $current_month_year;
}

// The query argument
$newsPrevArgs = array(
    'cat'               => 'news',
    'year'              => $prev_month_year,
    'monthnum'          => $prev_month_num,
    'order'             => 'DES',
    'posts_per_page'    => 5
);
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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