Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.