WordPress.org

Ready to get started?Download WordPress

Forums

how to display a random post (1 post)

  1. tades
    Member
    Posted 5 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic