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

I am loading single images into a post and calling their source from my custom post type template single-photo.php :

src: "<?php echo get_post_meta($post->ID, 'single_photo', true); ?>"

I want to pull an image from a random post only from that post type [PHOTO]. I know the url will be mysite.com/photo/-random-photo-, how could I grab a radom meta field?

share|improve this question

You could pull it directly form the database:

global $wpdb;

$random_photo = $wpdb->get_var("SELECT meta_value FROM wp_postmeta WHERE meta_key like 'single_photo' ORDER BY RAND()");
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.