WP - Random redirect
- Started
- Last post
- 4 Responses
- Jaline0
Bump for the night crowd.
- Jaline0
function random_redirect() {global $wpdb;
$random_id = $wpdb->get_var( "SELECT wp_posts.ID FROM $wpdb->posts, wp_term_relationships WHERE wp_posts.post_type = 'post' AND wp_posts.post_password = '' AND wp_posts.post_status = 'publish' AND wp_posts.ID = wp_term_relationships.object_id AND wp_term_relationships.term_taxon... = 22 ORDER BY RAND() LIMIT 1");
wp_redirect( get_permalink( $random_id ) );
exit;
}I had issues with it first latching onto a tag and only cycling through those specific posts, and then, when I altered some of it, it changed to just redirecting to the newest post.
- Jaline0
Maybe something like this:
<?php query_posts('showposts=1&cat=22,23,24,25&orderby=rand');?>
except in link format
- ernexbcn0
Try this (75,76,77 are your category IDs):
query_posts('orderby=rand&showpo...
if (have_posts()) :
while (have_posts()) : the_post();....
- QBN ate the code:
http://pastie.org/16…ernexbcn - thanks, giving it a go nowJaline
- QBN ate the code:
- Jaline
Wordpress question. I have an image that is supposed to redirect to a random post in specific categories (4 of them) when the user clicks on it. I have some PHP below, but I'm wondering if I could target specific categories (or exclude a certain category ID).
<?php $randomPost = $wpdb->get_var("SELECT guid FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY rand() LIMIT 1");
echo '<a href="'.$randomPost.'">Random Post</a>';
?>Doing it by post type would be kind of annoying. I'd have to go back and change about 200 posts.
After fiddling around with various methods, I tried the random redirect plugin:
http://wordpress.org/extend/plug…
but it weirdly only chooses one tag and cycles through the posts in it.Thanks for suggestions.