Random categories with random posts by Mike Stickel. Author: Ady Romantika Version: 1.5.0 Author URI: http://www.romantika.name/v2/ */ function ara_random_posts($before,$after) { global $wpdb; $options = (array) get_option('widget_ara_randomposts'); $title = $options['title']; $list_type = $options['type'] ? $options['type'] : 'ul'; $numPosts = $options['count']; $category = $options['cat']; # Articles from database $rand_articles = ara_get_random_posts($numPosts,$category); # Header $string_to_echo = ($before.$title.$after."\n"); switch($list_type) { case "br": $string_to_echo .= "

"; $line_end = "
\n"; $closing = "

\n"; break; case "p": $opening = "

"; $line_end = "

\n"; break; case "ul": default: $string_to_echo .= "\n"; } for ($x=0;$x 0 ) $string_to_echo .= $opening; $string_to_echo .= ''.$rand_articles[$x]['title'].''; if (strlen($line_end) > 0) $string_to_echo .= $line_end; } if (strlen($closing) > 0) $string_to_echo .= $closing; return $string_to_echo; } function ara_get_random_posts($numPosts = '5',$category = '') { global $wpdb, $wp_db_version; if($category == ''): $sql = "SELECT $wpdb->posts.ID FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post'"; else: if($wp_db_version >= 6124): // Database structure has changed since WP 2.3 $sql = "SELECT $wpdb->posts.ID "; $sql.= "FROM $wpdb->posts, $wpdb->term_relationships, $wpdb->term_taxonomy "; $sql.= "WHERE $wpdb->posts.post_status = 'publish' "; $sql.= "AND $wpdb->posts.post_type = 'post' "; $sql.= 'AND '; $sql.= '( '; $sql.= "$wpdb->posts.ID = $wpdb->term_relationships.object_id "; $sql.= "AND $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id "; $sql.= "AND $wpdb->term_taxonomy.term_id = $category "; $sql.= ')'; else: $sql = "SELECT $wpdb->posts.ID "; $sql.= "FROM $wpdb->posts, $wpdb->post2cat "; $sql.= "WHERE $wpdb->posts.post_status = 'publish' "; $sql.= "AND $wpdb->posts.post_type = 'post'"; $sql.= "AND $wpdb->post2cat.post_id = $wpdb->posts.ID "; $sql.= "AND $wpdb->post2cat.category_id = $category"; endif; endif; $the_ids = $wpdb->get_results($sql); $luckyPosts = (array) array_rand($the_ids,($numPosts > count($the_ids) ? count($the_ids) : $numPosts)); $sql = "SELECT $wpdb->posts.post_title, $wpdb->posts.ID"; $sql .= " FROM $wpdb->posts"; $sql .= " WHERE"; # Here we minimize number of query to the database by using ORs - just one query needed foreach ($luckyPosts as $id) { if($notfirst) $sql .= " OR"; else $sql .= " ("; $sql .= " $wpdb->posts.ID = ".$the_ids[$id]->ID; $notfirst = true; } $sql .= ')'; $rand_articles = $wpdb->get_results($sql); # Give it a shuffle just to spice it up shuffle($rand_articles); if ($rand_articles) { foreach ($rand_articles as $item) { $posts_results[] = array('title'=>str_replace('"','',stripslashes($item->post_title)), 'permalink'=>post_permalink($item->ID) ); } return $posts_results; } else { return false; } } function widget_ara_randomposts_control() { $options = $newoptions = get_option('widget_ara_randomposts'); if ( $_POST['randomposts-submit'] ) { $newoptions['title'] = strip_tags(stripslashes($_POST['randomposts-title'])); $newoptions['type'] = $_POST['randomposts-type']; $newoptions['count'] = (int) $_POST['randomposts-count']; $newoptions['cat'] = $_POST['randomposts-category']; } if ( $options != $newoptions ) { $options = $newoptions; update_option('widget_ara_randomposts', $options); } $list_type = $options['type'] ? $options['type'] : '