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?

1 Answer 1


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()");

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.