wordpress - How to show one random post from a specific post type for 24 hours then refresh -
i'm not sure if possible, wondering if there anyway create wordpress query select post @ 00:00 in morning , show post 24 hours. when hits 00:00 following morning should refresh , show anther random post 24 hours. possible, if how?
you can using wordpress transients api , wp_query class.
$random_id = ''; if ( false === ( $random_id = get_transient( 'some_random_post_id' ) ) ) { // transient expired, create $args = array( 'posts_per_page' => 1, #return 1 value 'orderby' => 'rand', 'post_type' => 'yourposttype' ); $single_post_query = new wp_query( $args ); while( $single_post_query->have_posts() ){ $single_post_query->the_post(); $random_id = get_the_id(); set_transient('some_random_post_id', $random_id, 60*60*24); # save id returned } } //do stuff $random_id post id.
note may not 24 hours since wordpress update transient when visits website. also, if using wordpress 3.5 or above may use constant day_in_seconds
instead of 60*60*24
.
Comments
Post a Comment