Haven’t we been over this before? Hmm…
Have you at all tried using query_posts() for this? I imagine you could set up a home.php template with the query initializing The Loop like so:
<?php
$current = date('Ym');
query_posts("m=$current&showposts=-1"); $wp_query->is_archive = false;
if(!have_posts()) {
query_posts("showposts=1"); $wp_query->is_archive = false;
}
?>
That queries all posts in the current month. If there are no posts, it then queries the last post available.
Thread Starter
Jack
(@moxie)
I posted this question before, you’re right. Just never got it to work properly :(. And I never heard of the query function before until now.
I tried your code, but this returns the last post, no matter what month I choose or which category. Perhaps I don’t really understand what is needed, but isn’t it a bit more complicated? If the $current = 200509 then just query_posts(“m=$current&showposts=100”); should return all the posts in that month (considered that that month has not more than 100 posts)??? But what if it has no posts? Than the previous month should be checked. But how to do that?
I don’t know how to write this in code, but my guess would be these steps:
– a query to find out the date of the last post
– in what month is that post?
– query_posts(“m=$current&showposts=100”);
Something like that?
“Something like that?“
If *that’s* what you want, here’s a quick way to do it:
<?php
$latest = $wpdb->get_row("SELECT MONTH(post_date) as month, YEAR(post_date) as year FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date DESC LIMIT 1");
query_posts('m=' . $latest->year . zeroise($latest->month, 2) . '&showposts=-1');
$wp_query->is_archive = false; $wp_query->is_home = true;
?>
Details:
$latest is value of database query collecting ‘year’ and ‘month’ of the last “published” post. The query_post() may look complicated now, but it’s not technically different from the version above (zeroise() merely assures month is in 01 format). Note -1 (minus one) for the ‘showposts’ parameter value; this in effect means “display everything queried.” Finally, as passing m= to query_posts resets the page query type to “archive,” the $wp_query statements get everything behaving as expected for your blog’s ‘home’ page.