How to automatically shift a Wordpress loop once a day?

Hello everyone!

I have another brain-buster to discuss. I've gotten fairly close to a possible solution, and need a bit of a nudge.

Feel free to suggest alternatives to my thinking.

So, I have a loop of posts. This loop is sorted by date in ascending order, and is only showing the most recent 5, counting up from "today".

So, to make is a bit easier to understand... today is 11/13/2014. The title of each post is 11/13/2014, 11/14/2014, 11/15/2014, 11/16/2014, and 11/17/2014...

I also have pre-written more posts that are not showing yet.

On 11/14/2014, I want to automatically shift my loop, so that 11/13/2014 drops off, and the loop now goes from 11/14/2014 - 11/18/2014.

I don't want to have to manually go into Wordpress and delete or turn the post to draft ... I want this shift to happen automatically, everyday at midnight.

I have discovered the 'offset' attribute, which successfully shifts the loop by the number you are offsetting. I was originally going to try and figure out how to increase the offset number everyday at midnight ...

Do you think this is a good way of achieving this goal? If so, can you point me in the right direction on how to automatically increment an integer everyday at midnight?

If you have an alternative, please let me know. I am up for suggestions at this point.

If you need more information to make an informed comment, please also let me know.


  • Tyler Postle

    Hey Media,

    Hope you're doing well today and thanks for your question!

    I think I might be a bit confused here. So you have the 5 most recent posts showing and each day you want to bump one of them off and replace it with a more recent one?

    I know I must be misunderstanding this because this is how WordPress works by default if you set the blog to 5 posts, Then it will always keep the 5 most recent posts(assuming you have it set that way).

    Do you have posts scheduled to be posted? and each night you want that days post to be posted and push the oldest post off?

    If you schedule your posts with the built in scheduler and have the post limit set to 5 then should it not be doing that as you want?

    Hopefully I'm not completely missing the boat on your question here. Looking forward to discussing this further Media!

    All the best,

  • mediastead


    Thanks for the input!

    Normally I would agree with you, however, in your scenario, the 5 most recent posts would be sorted by date and in descending order, correct?

    I had it that way, but then the days are counting backwards, and not forward the way I need them to count.

    So, in your scenario, if the top post is TODAY, and lets say TODAY is FRIDAY ... it would go down the page 5 posts as FRIDAY, THURSDAY, WEDNESDAY, TUESDAY, MONDAY.

    I need the days to count up into the future ... so, TODAY is FRIDAY, and I need it to go FRIDAY, SATURDAY, SUNDAY, MONDAY, TUESDAY.

    To put it into perspective for you, I am building a 5-day weather forecast page, where my client wants to create a bunch of forecasts in advance (probably 2 weeks worth) ... He doesn't want to manually rotate them ...

    I set up a custom post type, where every post is a day. He is making the post title the day of the week (like Monday, 11/17/2014) ... HOWEVER, it is likely that 10 - 14 days of forecast will be posted in the same day.

    We can't schedule each forecast to be posted on the day of the forecast, because we have to show 5 days worth at once.

    Is this clearer now?

    Normally, I would absolutely use sort by date, descending, and have them push down as the days pass, but I can't figure out a way to have them pull up ... does that make sense?

    I hope I am being clear...

  • mediastead

    So, here is what I came up with ...

    I just created a ton of posts to test this out.

    I started the posts as 11/13/2014 and I create until 11/20/2014...

    I just downloaded a plugin called "post expirator"

    I am going to make the oldest post expire and turn into a draft at midnight of the next day.


    Okay ... my first day in the 5-day forecast is today ... 11/13/2014.

    I set the post expirator to turn today's post into a draft at midnight.

    This should effectively remove 11/13/2014, and pop 11/14/2014 to the top of the page and then the posts will count down from there.

    Then, I will have 11/14/2014 expire at midnight of 11/15/2014, so 11/14/2014 is removed, and 11/15/2015 becomes the first day in the list.

    In theory, this all sounds decent.

    Unfortunately, there is still some manual setting up, but it seems to be a good solution for now.

    If you can think of a way to get the same effect with some programming, keep me posted.

    I'd love ot know what you think either way.


  • Tyler Postle

    Hey Mediastead,

    Thanks for your replies!

    I'm wondering if sorting it numerically would and ascending would work? Then it would just go up based on the dates. If it's in dd/mm/yy format that it would always be increasing.

    This article could help with that:

    Would something like that work? :slight_smile:

    Look forward to hearing back!

    All the best,

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.