Select a random post from a specific category?

Question

I’m very new to word press having only installed it on the weekend, I’m fine with HTML & CSS but PHP & WordPress is alien to me.

I’m trying to display a post from a specific category that changes every day, i’ve searched everywhere and found the below code whihc works to a point.

I just cant seem to get it to choose from a category number?

Any help or explanation would be much appreciated.

    <?php
if ( false === ( $totd_trans_post_id = get_transient( 'totd_trans_post_id' ) ) ) {
     $args = array('numberposts' => 1, 'orderby' => 'rand');
     $totd = get_posts($args);
     $midnight = strtotime('midnight +1 day');
     $timenow = time();
     $timetillmidnight = $midnight - $timenow;
     echo $midnight;
     echo ",".$timenow;
     set_transient('totd_trans_post_id', $totd[0]->ID, $timetillmidnight);
} else {
    $args = array('post__in' => array($totd_trans_post_id));
    $totd = get_posts($args);
}

foreach( $totd as $post ) : setup_postdata($post); ?>
    <div>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        <?php the_content(); ?>
    </div>
<?php endforeach; ?>

EDIT Thanks for the suggestions but nothing seems to be working with my existing code, it still continues to pick a completey random post from all categories.

--------------------------------------------------

>>> Share a link to this question, just copy and paste the code bellow on your web page <<<

<a href="https://www.wptricks.com/question/select-a-random-post-from-a-specific-category/">

Select a random post from a specific category?</a>
0
, BuzzinBilly 3 years 0 Answers 40 views 0

Leave an answer