The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

Random post on front page (1 post)

  1. ArchStanton
    Posted 9 years ago #

    I'm trying to display one random post on the front page.

    I put this code before The Loop and it works well, except it doesn't display comments or split posts with More tag.

    $rand_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY RAND() LIMIT 1");

    I also tried this code, but it didn't work at all. Maybe I just didn't implement it correctly.

    global $wpdb;
    $numposts = 1;
    $rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY RAND() LIMIT $numposts");
    foreach($rand_posts as $post) :
    <?php endforeach; ?>

    Can someone please tell me how to get either one of these working correctly?


Topic Closed

This topic has been closed to new replies.

About this Topic