WordPress.org

Support

Support » How-To and Troubleshooting » how to display a random post

how to display a random post

  • I have this code to show random post, but I can not configure it to display a specific category

    <?php
    $rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_status = 'publish' AND post_type='post' ORDER BY RAND() LIMIT 1");
    foreach($rand_posts as $post){
    setup_postdata($post);
    ?>
    <h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    
    <? }
    ?>

    I saw this information, but not much understanding.

  • The topic ‘how to display a random post’ is closed to new replies.