WordPress.org

Ready to get started?Download WordPress

Forums

Plugin Request: Recent MONTH as startpage instead of home (4 posts)

  1. Jack
    Member
    Posted 8 years ago #

    I was looking for a solution in another forum but got no reply. I don't think this would be a terribly difficult to write plugin, but I for myself am not succeeding in doing so. Better said: I don't even know where to begin :)

    This is the post: http://wordpress.org/support/topic/41808#post-234846

    and the request is for a plugin that would make the blog start with the latest month that has posts instead of the last 5 - 10 posts. In this case the blog would start with all the posts in the latest month that actually has posts, being 20 posts or just one, but giving me in the same time the opportunity to add the months navigation right away.

  2. Kafkaesqui

    Posted 8 years ago #

    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.

  3. Jack
    Member
    Posted 8 years ago #

    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?

  4. Kafkaesqui

    Posted 8 years ago #

    "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.

Topic Closed

This topic has been closed to new replies.

About this Topic