How To Get Random WordPress Post Once Per Day

I would like to display a random post on the homepage each day.

You can use WordPress Transients API to get the post once and then cache it for 24 hours.
See following example

if ( false === ( $posts = get_transient( 'random_post' ) ) ) {
  $args = array(
     'post_type' => 'post',
     'orderby'   => 'rand',
     'posts_per_page' => '1'
  );
  $posts = get_posts( $args );
  set_transient( 'random_post', $posts, DAY_IN_SECONDS );
}
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments