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 pull a single random image from custom post type "athletes" and the custom image field "athletethumbnail" to be displayed in my sidebar? (I have PHP enabled for my text widget on sidebar.)

share|improve this question
up vote 1 down vote accepted

If I get your question right, as you mention Tag in the title, but not in the Content - and also mention a random image but then it's not a library image but a custom field value...

So, the following code will grab 1 random custom post type that has a custom field value that's not empty. And it's a mix of get_posts, get_post_meta and Custom_Field_Parameters:

$args = array(
   'numberposts' => 1,
   'orderby' => 'rand',
   'post_type' => 'athletes',
   'post_status' => 'publish',
    'meta_query' => array(
            'key' => 'athletethumbnail',
            'value' => '',
            'compare' => '!=' /* will not grab posts with empty Custom Field */

$show_albums = get_posts ( $args );
echo get_post_meta($show_albums[0]->ID,'athletethumbnail',true);
share|improve this answer

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.