WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How would I make a widget to display a random post's image and permalink? I would like it to pull from a specific custom content type and only posts have set the custom field of "featured" to "yes".

share|improve this question
<?php query_posts( 'cat=36&post_type=athletes&showposts=1&orderby=rand'); ?>

changed to :

$args = array( 'numberposts' => 36,'category'  => '1','post_type'  => 'athletes', 'orderby' => 'rand');

$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php endforeach; ?>
share|improve this answer
Don’t use query_posts() if you don’t want to overwrite the main query. – toscho Jun 24 '12 at 18:22
@toscho I changed my code (see edit) but now nothing is displaying. Title of unrelated post. – Sam Jun 24 '12 at 18:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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