I have my loop, which unfortunately only captures all posts PRIOR to the current month. And so I end up with posts in the current month (April 2015) that aren’t showing.
How do I get the loop to include the current month posts as well?
My code:
<?php function the_archive () {
global $wpdb;
$limit = 0;
$year_prev = null;
$months = $wpdb->get_results("SELECT DISTINCT MONTH( post_date ) AS month , YEAR( post_date ) AS year, COUNT( id ) as post_count FROM $wpdb->posts WHERE post_status = 'publish' and post_date <= now( ) and post_type = 'post' GROUP BY month , year ORDER BY post_date DESC");
foreach($months as $month) :
$year_current = $month->year;
if ($year_current != $year_prev) {
if ($year_prev != null) {} ?>
<li class="archive-year">
<a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/">
<?php echo $month->year; ?>
</a>
</li>
<?php } ?>
<li class="archive-month">
<a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/<?php echo date("m", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>/">
<?php echo date_i18n("F", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>
</a>
</li>
<?php $year_prev = $year_current;
if(++$limit >= 18) { break; }
endforeach;
}
add_shortcode( 'show_archive' , 'the_archive' );
?>
The above outputs a menu to navigate archive posts – see: http://imgur.com/C4WIiKR & http://codeandco.net/news/
So I’m expecting to see a menu item for APRIL, but I do not.
Any ideas?
(thanks in advance 🙂
So I figured this out. The code actually works fine, but there is a small issue possibly due to timezones (I’m guessing).
As I am in Australia, the USA (where maybe WordPress’s core dates are calculating from perhaps) is behind in time. So I posted my post being today, 6th April 2015 – but it wasn’t appearing.
When I changed the published date on the post, to ONE day before (the 5th April 2015), it worked as expected with no issues!
This boils down to a simple fact that in WordPress: you can’t publish posts that are dated in the future. They can only be ‘scheduled’ to be posted at a future date.