Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY000]: General error: 145 Table './skippy_skippy/h_sessions' is marked as crashed and should be repaired in system/classes/databaseconnection.php line 269
I wrote up a quickie hack for Sushubh to pull random blog entries from the database. To make it generally useful, I wrote it as a function that takes two arguments:
random_posts ($catID = 0, $count = 4);
By default, it'll grab four random posts from any category. Give it a category ID and/or a specific number of posts to pull in order to change it.
If you know you're not going to use all of the posts' data, you could tweak the last SQL query to grab just the bits you need, instead of everything, thereby easing the load on your database.
Oh yeah, this hack does not honor my per-post user-level restrictions modification. If you're using that, this hack will happily display all your restricted posts.
Not the most useful of hacks, but maybe someone somewhere will put it to good use.
UPDATE: I've made this into a proper plugin for WordPress 1.5 and above. To use it, simply install and activate the plugin, then insert the following code in your template:
<?php
echo "<ul>";
$rp = random_posts($catID, $count);
foreach ($rp as $p) {
$title = get_the_title($p);
$link = get_permalink($p);
echo "<li><a href='$link' title='$title'>$title</a></li>";
}
echo "</ul>";
>
Download random-posts.zip!
About | Policies | skippy.net
Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY000]: General error: 145 Table './skippy_skippy/h_sessions' is marked as crashed and should be repaired in system/classes/databaseconnection.php line 269
Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY000]: General error: 145 Table './skippy_skippy/h_sessions' is marked as crashed and should be repaired in system/classes/databaseconnection.php line 269
on the line above The Loop. It worked. It called random posts from category 4. However, I wanted it to show 12, and it showed 11, or 9, or 8. Everytime I refreshed it showed a different number of posts. If you could throw some clues my way I'd appreciate it. Thanks.<?php $p = random_posts('26', '1'); query_posts("p=$p"); echo ($p); ?> <?php while (have_posts()) : the_post(); ?> <p><?php the_title(); ?></p> <?php the_content(); ?> <?php endwhile; ?>but i just get a list of posts, from no particular category. and i tried putting single quotes around the category ID, and number and removing them too. no difference.. when i echo the $p variable i get Array... oh, and i tried the code above but i need to get the content for the post, and can't figure out how to do that. a anyone got a clue to spare?print_r($p)to see what the array contains. Then you can use$p['foo']to access the individual array elements.index.php."; $rp = random_posts($catID, $count); foreach ($rp as $p) { $title = get_the_title($p); $link = get_permalink($p); echo "$title"; } echo ""; >above the Loop in index.php?"; $rp = random_posts($catID, $count); foreach ($rp as $p) { $title = get_the_title($p); $link = get_permalink($p); echo "$title"; } echo ""; >My error is: Parse error: parse error, unexpected '>' in /www/htdocs/w005f3de/wp-content/themes/ski/home.php on line 58 I actually want to display a random post from a specif category, not as title but as the actual post. how do I do it? thanksID; query_posts($query); if (have_posts()) : while (have_posts()) : the_post(); ?>... ususal standard loop thingies ...I suppose there should be a better way (not a work-around like this) but that's for others to figure out :)$sql = "SELECT * FROM $wpdb->posts WHERE "; $list=$result ; #range(0,$max); shuffle($list); $list=array_slice($list,0,$count); for ($i = 0; $i < $count; $i++) { if ($i == '0') $sql .= "(ID = '" .$list[$i]. "')"; #. $result[rand(0,count($result) -1)] . "')"; else $sql .= "OR (ID = '" .$list[$i]. "')"; #$result[rand(0,count($result) -1)] . "')"; }if($preformat) { $formatted= ''; foreach ($randomposts as $p) { $title = get_the_title($p); $link = get_permalink($p); $formatted.= "<h5><a href='$link' title='$title'>$title</a></h5>";} echo $formatted; return true; }...where $preformat is a third variable passed to the function as either true or false.$rp = random_posts(4, 1); foreach ($rp as $p) { $title = get_the_title($p); $link = get_permalink($p); echo " $title "; }Although now I see you'd need to use the loop or a hack of get_the_content so that a permalink could be passed. Well, the random muse appears to be working on your blog. Is it an illusion or how did you solve it?$sql .= " WHERE post_status='publish' ORDER BY ID ASC";and see if that produces better results.