Random post, once per day

First of all you really shouldn’t be using query_posts(). Read this excellent explanation why.

Then this is a perfect use case for transients.

You just get the post once and then cache it for 24 hours using the Transients API.

if ( false === ( $quotes = get_transient( 'random_quote' ) ) ) {
  // It wasn't there, so regenerate the data and save the transient

  $args = array(
     'post_type' => 'quote',
     'orderby'   => 'rand',
     'posts_per_page' => '1'
  );

  $quotes = get_posts( $args );

  //Now we store the array for one day.
  //Just change the last parameter for another timespan in seconds.
  set_transient( 'random_quote', $quotes, DAY_IN_SECONDS );
}

If your goal was to have it bound to calendar days and not 24 hours from last request replace the last line with the following. (props to chaos)

$seconds_until_next_day = strtotime('tomorrow') - time();
set_transient( 'random_quote', $quotes, $seconds_until_next_day );

After you have got the data you can just display it any way you want:

foreach ( $quotes as $post ) : setup_postdata( $post );

  [...]
  the_title();
  [...]

endforeach; 
wp_reset_postdata();

See this link for more examples.

EDIT:

This should do the trick in your particular situation:

foreach ( $quotes as $post ) : setup_postdata( $post );

    if ( has_post_thumbnail() ) {
                the_post_thumbnail( 'thumbnail-quote' );
    } else { ?>
            <img src="https://wordpress.stackexchange.com/questions/199619/<?php echo get_stylesheet_directory_uri (); ?>/img/thumb1.jpg" alt="x" class="quote_person" />
    <?php } ?>
    <div class="quote_container">
        <span class="quote_intro hstack">Quote of the day:</span><a class="quote_tweet hstack" href="#">Tweet this quote <i class="fa fa-twitter"></i></a>
        <div class="quote_message white gstack"><?php the_field('quote'); ?></div>
        <div class="quote_source white hstack"><?php the_field('attribution'); ?></div>
    </div>

<?php endforeach; 
wp_reset_postdata();

Leave a Comment