How do I create a random post that will last for a day

How do I create a random post but it shouldnt be change when the page is refreshed

I want a random post that will last for a day and after 24 hours or a day,
it will change randomly the post ,
will be that possible? or are there a plugin that exactly does that ,

actually what Im trying to create is like a “Random Tip For the Day”, you know something like that

I hope someone understand my question

please help,any help will be appreciated.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Here is some code doing what you’ve asked and using the ideas others have highlighted:

<?php
if ( false === ( $totd_trans_post_id = get_transient( 'totd_trans_post_id' ) ) ) {
     $args = array('numberposts' => 1, 'orderby' => 'rand');
     $totd = get_posts($args);
     $midnight = strtotime('midnight +1 day');
     $timenow = time();
     $timetillmidnight = $midnight - $timenow;
     echo $midnight;
     echo ",".$timenow;
     set_transient('totd_trans_post_id', $totd[0]->ID, $timetillmidnight);
} else {
    $args = array('post__in' => array($totd_trans_post_id));
    $totd = get_posts($args);
}

foreach( $totd as $post ) : setup_postdata($post); ?>
    <div>
        <a href="<?php the_permalink(); ?>" rel="nofollow noreferrer noopener"><?php the_title(); ?></a>
        <?php the_content(); ?>
    </div>
<?php endforeach; ?>

get_posts() help Plus the way I have coded it should make the Tip of the Day post change at midnight every day.

This can be improved upon because we are showing random there is nothing stopping the same post showing twice in a row…

Method 2

A transient seems like a lot of extra effort. Instead, just use the fact that MySQL’s RAND function can be seeded for reproducible random numbers.

Use a function like this:

function force_random_day_seed($orderby) {
    $seed = floor( time() / DAY_IN_SECONDS );
    $orderby=str_replace('RAND()', "RAND({$seed})", $orderby);
    return $orderby;
}

Then, around your WP_Query or get_posts or whatever it is that you have retrieving this single post, use this filter code:

add_filter('posts_orderby', 'force_random_day_seed');
$args = array('numberposts' => 1, 'orderby' => 'rand');
$totd = get_posts($args);
remove_filter('posts_orderby', 'force_random_day_seed');

This adds the filter, gets the post, then removes the filter.

By setting the seed to a number that changes once a day, then the random will produce the same answer every time for that day. Then it will give a different random number every DAY_IN_SECONDS goes by.

No transients, no need for storage of data.

Update for WordPress 4.5+

Ordering by random, containing a seed value, is now supported in WordPress 4.5+:

$rand = sprintf( 'RAND(%d)', floor( time() / DAY_IN_SECONDS ) );
$args = array( 'numberposts' => 1, 'orderby' => $rand );
$totd = get_posts($args);

See ticket #35692 for more information.

Method 3

You can use the Transients API, as kaiser said. Just store a random post ID with a lifetime of 24h. To select a random post you can set the orderby argument of get_posts() to "rand".

Use case example:

function wpse17056_twentyfour_sticky_post()
{
    $transient = get_transient( 'twentyfour_sticky_post' );

    // set transient if none is present    
    if ( false === $transient ) 
    {
        $rand_post = get_posts( array( 'numberposts' => 1, 'orderby' => 'rand') ); 
        set_transient( 'twentyfour_sticky_post', $rand_post[0]->ID, DAY_IN_SECONDS );
    }
}
add_action( 'init', 'wpse17056_twentyfour_sticky_post', 0 );

Method 4

Interact with the Transients API. You could attach something like “sticky” to it or a specific category or term.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x