Hoping someone can help - for some reason my brain is mush on this one and I can't seem to find a similar example via Googling.

What I have is a CPT called "products" for example and what I need to do is every day I need to display 1 product from this CPT, cycling through each product daily in alphabetical order based on post title.

So for example if I had the following posts: Apples, Bananas, Oranges

If I implemented it today it would show the following post on each day: May 18th - Apples
May 19th - Bananas
May 20th - Oranges
May 21st - Apples
May 22nd - Bananas

Thoughts? Help?

  • Maybe as a starting point calculate the number of days from your first date and then take the modulus on the total number of posts like: $days_since_start % $posts->post_count. You could then use this as an offset to show the nth post. May 18 '16 at 13:17
  • Would you mind giving a more complete code example of what you are suggesting? Obviously this wont be alphabetically but it is an option to consider.
    – qbattersby
    May 18 '16 at 16:54

First, calculate the number of whole days from your start date, then take the modulus of this and the total number of posts in 'products'. This will give a 'post number' which you can use in the loop to show only the relevant post.

So, if you have three posts $days_elapsed % $post_count would look like:

| days | mod |
|  0   |  0  |
|  1   |  1  |
|  2   |  2  |
|  3   |  0  |
|  4   |  1  | etc...

Since the post index number (post->current_post) starts at zero, you can use an if statement to display the correct post.

$start_date = new DateTime("2016-05-18");
$now = new DateTime("now");
$days_elapsed = $now->diff($start_date)->format("%a");

$args = array (
    'post_type' => array( 'products' ),
    'order'     => 'ASC',
    'orderby'   => 'title',

$query = new WP_Query( $args );

if ( $query->have_posts() ) {while ( $query->have_posts() ) {$query->the_post();
        if ($post->current_post == $days_elapsed % $query->post_count) {

Note: this is untested, but should get you started. There may also be a more elegant way to do this than loop through all the posts in products

  • This is extremely helpful and I started down this path last night. A question you may be able to help with. After we are more days away from where we started than there are posts it would meet the if statement. Do you have a suggestion for reseting the $start_date?
    – qbattersby
    May 19 '16 at 13:19
  • You could always just use the days since the unix epoch - something like: round( time() / 86400, 0, PHP_ROUND_HALF_DOWN) May 19 '16 at 13:40
  • Hmmm Im not sure that would solve it - what I need to do is ensure every product gets shown that is the key here.
    – qbattersby
    May 19 '16 at 14:08
  • It should work fine: if you have 4 posts and are on day 26, then 26 % 4 = 2, which would show your 3rd post, 'oranges'. The next day, 27 % 4 = 0 which would show the first post, 'apples'. Conversely, if we're on day 3 and you have 1800 posts, 3 % 1800 = 3 and would show the 4th post. May 19 '16 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.