WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] How to display current month’s posts only?

[Resolved] How to display current month’s posts only?

  • In my index.php, I’d like to display only the current month’s posts, and if there are no posts in the current month, then keep going back a month until some posts are found.

    Here’s a first crack at it:

    This code goes in index.php just before the Loop starts:

    $m = date(‘m’);
    $y = date(‘Y’);
    query_posts(“monthnum=”.$m.”&”.”year=”.$y);
    while (!have_posts())
    {
    $m = $m-1;
    if ($m == 0)
    {
    $m = 12;
    $y = $y – 1;
    }
    query_posts(“monthnum=”.$m.”&”.”year=”.$y);
    }

    Any comments/suggestions welcome!

Viewing 5 replies - 1 through 5 (of 5 total)
  • I know this is an age old problem for a lot of people.. I want the same thing but for a week, and I’ve not found a solution yet.

    I’ll mention it here – but if people think I should start a new thread I will.

    I run a magazine using wordpress – I currently use 1 day to show weekly posts, because we update once a week – however I really want to be able to update a few times over the week – but still have exactly 1 week’s (in calendar terms – not 7 days where there have been posts)

    If there is no posts in a week – I want the previous week in calendar terms to show up. If it helps I post all posts into an issue subcategory (of the issue date) which also represents a week’s issue.

    current week might be tricky… not sure if query_posts takes such an argument. But with a little research and some PHP code, you could do it. I’ve got enough on my hands at the moment… sorry I cannot offer an example.

    You might want to refer to an existing support thread on this issue.

    I had looked at that thread, but it was not very clear…

    The code I posted seems to be working, however…

    I borrowed this code, and I works well, but unfortunately, the code I have to offer up the “See More” (previous month) link isn’t working.

    UPDATE: Figured it out.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] How to display current month’s posts only?’ is closed to new replies.
Skip to toolbar