The following lines of codes will let you redirect all your users to a random post in WordPress.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // set arguments for get_posts() $args = array( 'numberposts' => 1, 'orderby' => 'rand' ); // get a random post from the database $my_random_post = get_posts ( $args ); // process the database request through a foreach loop foreach ( $my_random_post as $post ) { // redirect the user to the random post wp_redirect ( get_permalink ( $post->ID ) ); exit; } |
Snippet Source/Credit: Smashing Magazine