Skip to content

Commit

Permalink
Use 'orderby' => 'rand' WP Query to get a random bot.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbohacek committed May 10, 2018
1 parent 33d53c6 commit 508543d
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions includes/random-bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ public function __construct() {
public function show_random_bot( $content ) {
global $wp_query;
if ($wp_query->query['name'] === 'random-bot'){
$all_bots = new WP_Query(array(
$random_bot = new WP_Query(array(
'post_type' => 'bot',
'post_status' => 'publish',
'posts_per_page' => -1,
'ignore_sticky_posts'=> true
'posts_per_page' => 1,
'orderby' => 'rand'
));
$random_bot = $all_bots->posts[array_rand($all_bots->posts)];
$random_bot_url = get_post_permalink($random_bot->ID);
wp_redirect($random_bot_url);
wp_redirect( get_post_permalink($random_bot->post->ID) );
}
}
}
Expand Down

0 comments on commit 508543d

Please sign in to comment.