Last active
August 29, 2015 14:27
-
-
Save nickkoskowski/2fb00a84b2be7f91cb30 to your computer and use it in GitHub Desktop.
Get a random post link from WP database using query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function get_random_post_link() { | |
$args = array( | |
'orderby' => 'rand', | |
'posts_per_page' => '1', | |
); | |
$wp_query = new WP_Query( $args ); | |
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); | |
$postlink = get_the_permalink(); | |
endwhile; endif; wp_reset_query(); | |
return $postlink; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
May I suggest to initialize the $postlink that in case no posts where found you'll get Notice: Undefined variable: postlink in