I'm developing a home page that is a mishmash of a bunch of information, including a random smattering of several categories, including blog posts (category 192 in the code below)

I'm obviously not invoking the standard WP loop to do this. Is there a way to get the date and author to echo properly:

<?php $rand_posts = get_posts('cat=192&numberposts=2&orderby=rand'); foreach( $rand_posts as $post ) : ?>
<div class="box tall blogBox">
    <h2>Blog <span>| <?php the_date('m/d/Y'); ?></span></h2>
    <a href="<?php the_permalink() ?>"><img src="<?php the_field('01HomeThumbnail'); ?>" alt="<?php the_title(); ?>" width="113" height="111" /></a>
    <h3>by <?php the_author(); ?></h3>
    <h4><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
    <a target="_blank" href="<?php the_permalink() ?>" class="readPost">Read Post</a>
<?php endforeach; ?>

The two issues I'm experiencing I think are both because I'm outside of the loop, but unfortunately, I'm not sure how to get the info I need.

Basically, I am randomize 2 blog posts. Everything outputs as expected, except the date and the author. I know <?php the_author(); ?> is deprecated, but it's not returning the expected author anyway. The other thing not working is the_date. It seems to output on the first outputted blog post, but subsequent are blank.

Any help would be appreciated on either of these.

I'm building this on a custom theme based on the twenty ten theme.

1 Answer 1


The key to this is covered on the get_posts codex page: Access all post data using the setup_postdata function:

foreach ( $rand_posts as $post ) :  setup_postdata($post);

Regarding the_date, see the note about multiple instances on a page:

SPECIAL NOTE: When there are multiple posts on a page published under the SAME DAY, the_date() only displays the date for the first post (that is, the first instance of the_date()). To repeat the date for posts published under the same day, you should use the Template Tag the_time() or get_the_date() (since 3.0) with a date-specific format string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.