Support » Plugins and Hacks » display random posts on index page

Viewing 15 replies - 1 through 15 (of 16 total)
  • The string you have there is just the permalink code to a post. It would appear long before you collected your posts from the database, random or not.

    Could you provide a little more detail on what you’re attempting to change? If you must post code, submit it here:

    http://wordpress.pastebin.ca

    and reply with the url to it.

    Hi Kafkaesqui,

    I want to display random posts on the front page instead of showing latest posts. The reason I do this is because I seldom update my blog

    So you want to replace The Loop with something generating a random selection of posts?

    Not too hard:

    <?php
    global $wpdb;
    $numposts = 10;

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

    Template tags and whatnot go here.

    <?php endforeach; ?>

    Just change the value of $numposts to define how many posts to query for.

    I would like to specify categories to include and exclude. Thanks.

    The code above also shows pages. How can I exclude it? Thanks.

    use this loop to select a random post from a specific category.. remember to change the number of posts you want an dhte category name.

    <?php
    global $wpdb;
    $numposts = 1;
    $rand_posts = $wpdb->get_results(“SELECT * FROM $wpdb->post2cat,$wpdb->posts,$wpdb->categories WHERE
    $wpdb->post2cat.post_id = $wpdb->posts.ID and $wpdb->post2cat.category_id = $wpdb->categories.cat_ID and $wpdb->posts.post_status = ‘publish’ and $wpdb->categories.cat_name = ‘featured’ ORDER BY RAND() LIMIT $numposts”);
    foreach($rand_posts as $post) :
    setup_postdata($post);
    ?>

    I search exactly the same thing but… for pages (in fact for child pages of a specific parent page. There is an old plugin but it doesn’t make the job correctly (all text is whithout breaks and formatting and the title isn’t shown. See : http://www.aviransplace.com/index.php/get-random-page-wordpress-plugin/).

    I’m 0 for code. So if there si someone good, please help… !
    Thanks in advance 🙂

    <?php
    global $wpdb;
    $numposts = 1;
    $rand_posts = $wpdb->get_results(“SELECT * FROM $wpdb->post2cat,$wpdb->posts,$wpdb->categories WHERE
    $wpdb->post2cat.post_id = $wpdb->posts.ID and $wpdb->post2cat.category_id = $wpdb->categories.cat_ID and $wpdb->posts.post_status = ‘publish’ and $wpdb->categories.cat_name = ‘featured’ ORDER BY RAND() LIMIT $numposts”);
    foreach($rand_posts as $post) :
    setup_postdata($post);
    ?>

    Hi Everyone, this code works find and I am now getting random posts for category “featured”

    How do I take this one step further and show random post from whichever category im currently in?

    Now if I click on another category other than featured i still get the ramdom posts from featured.

    On home page it would show randon form every category, and when in a category it would show category specific

    Thanks a lot bcbirk100, but is it possible to display multiple categories?

    I want to know the same thing as babyuniverse. How do you diplay random posts in a category?

    Hey, it’s still showing the pages 🙁

    To only show posts, insert this code:

    post_type = 'post'

    Can somebody tell me exactly where you place this code in The Loop?

    <?php
    global $wpdb;
    $numposts = 1;
    $rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->post2cat,$wpdb->posts,$wpdb->categories WHERE
    $wpdb->post2cat.post_id = $wpdb->posts.ID and $wpdb->post2cat.category_id = $wpdb->categories.cat_ID and $wpdb->posts.post_status = 'publish' and $wpdb->categories.cat_name = 'featured' ORDER BY RAND() LIMIT $numposts");
    foreach($rand_posts as $post) :
    setup_postdata($post);
    ?>

    Template tags and whatnot go here.

    <?php endforeach; ?>

    thanks so much Archstaton

    2.3 breaks this solution – can anybody please whip up some code that works with 2.3?

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘display random posts on index page’ is closed to new replies.