Title: Show Random Posts Custom Fields
Last modified: August 19, 2016

---

# Show Random Posts Custom Fields

 *  Resolved [markdw](https://wordpress.org/support/users/markdw/)
 * (@markdw)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/show-random-posts-custom-fields/)
 * Hi there everyone. I have searched for this and found stuff all around what I
   am looking for, but nothing on this exact thing, so hopefully someone will be
   able to help.
 * I have a section of my home page that brings in the latest post from a certain
   category. Only I have got it showing the not post, but instead I have it showing
   a field from the custom fields. This field is an image.
 * So I have an image from the latest posts custom field, which I have called ‘Thumbnail’
   in a certain category showing on the home page.
 * What I want to do is to randomize this, so that every time a visitor goes to 
   the home page it will show a random custom field image from a post in a specific
   category.
 * Below is the code that I am using to generate latest post from a specific category
   and show the custom field called Thumbnail. Perhaps this could be adapted slightly
   to randomize the post shown.
 *     ```
       <?php $recent = new WP_Query("cat=1&showposts=1"); while($recent->have_posts()) : $recent->the_post();?>
       			<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_content_limit(200, ""); ?></a><div style="clear:both;"></div>
       			<?php endwhile; ?>
       ```
   
 * Thanks

Viewing 10 replies - 1 through 10 (of 10 total)

 *  Thread Starter [markdw](https://wordpress.org/support/users/markdw/)
 * (@markdw)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/show-random-posts-custom-fields/#post-749123)
 * Sorry I pasted the wrong piece of code (that was the div above!) Here it is:
 *     ```
       <?php $recent = new WP_Query("cat=1&showposts=1"); while($recent->have_posts()) : $recent->the_post();?>
       			<a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php echo get_post_meta($post->ID, "Thumbnail", true); ?>" alt="<?php echo get_post_meta($post->ID, "Theme Name", true); ?> thumbnail" /></a>
       			<?php endwhile; ?>
       ```
   
 *  [nickohrn](https://wordpress.org/support/users/nickohrn/)
 * (@nickohrn)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/show-random-posts-custom-fields/#post-749138)
 * Hey there. You should be able to do an orderby rand to get what you want.
 *     ```
       <?php $recent = new WP_Query("cat=1&showposts=1&orderby=rand"); while($recent->have_posts()) : $recent->the_post();?>
       			<a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php echo get_post_meta($post->ID, "Thumbnail", true); ?>" alt="<?php echo get_post_meta($post->ID, "Theme Name", true); ?> thumbnail" /></a>
       			<?php endwhile; ?>
       ```
   
 * Let me know if that worked.
 *  Thread Starter [markdw](https://wordpress.org/support/users/markdw/)
 * (@markdw)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/show-random-posts-custom-fields/#post-749142)
 * Thanks for that but it doesn’t seem to work for me. I am not getting any errors
   but it still just displays the most recent post from that category.
 * Any ideas?
 *  [nickohrn](https://wordpress.org/support/users/nickohrn/)
 * (@nickohrn)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/show-random-posts-custom-fields/#post-749143)
 * I’m sorry, I thought that would work. I got the information for that from this
   page: [http://codex.wordpress.org/Template_Tags/query_posts#Orderby_Parameters](http://codex.wordpress.org/Template_Tags/query_posts#Orderby_Parameters)
 * Maybe you could mess around with some more of the parameters there. Also, I know
   this is probably very obvious, but did you make sure you have more than one post
   in that category already? I’ve made that mistake when trying to get random stuff
   on other sites before.
 * If you do find a solution, be sure to post it here.
 *  Thread Starter [markdw](https://wordpress.org/support/users/markdw/)
 * (@markdw)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/show-random-posts-custom-fields/#post-749148)
 * Yes definitely more than one post in the category. Don’t worry as I have made
   these simple errors before.
 * I will have to have a play and maybe change the way that the database is queried?
 *  Thread Starter [markdw](https://wordpress.org/support/users/markdw/)
 * (@markdw)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/show-random-posts-custom-fields/#post-749231)
 * Finally figured it out. I am using the following code and it is working:
 *     ```
       <?php $rand_posts = get_posts('cat=3&numberposts=1&orderby=RAND()'); foreach( $rand_posts as $post ) : ?>
   
       <a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php echo get_post_meta($post->ID, "homeimage", true); ?>" alt="<?php echo get_post_meta($post->ID, "Theme Name", true); ?> homeimage" /></a>
   
       <?php endforeach; ?>
       ```
   
 * So just to clarify this brings in the random post form category number 3. It 
   shows 1 post and only shows the custom field names ‘homeimage’ which is an image.
   This image then links to the post itself.
 * Hope this helps others.
 *  [nickohrn](https://wordpress.org/support/users/nickohrn/)
 * (@nickohrn)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/show-random-posts-custom-fields/#post-749265)
 * Thanks for posting back here. I was attempting to do much the same you were doing
   here on a website I just completed development for. The solution I pointed out
   worked for me, and I’m surprised it didn’t work for you, too. But thanks for 
   posting your solution!
 *  [abelq](https://wordpress.org/support/users/abelq/)
 * (@abelq)
 * [17 years, 12 months ago](https://wordpress.org/support/topic/show-random-posts-custom-fields/#post-749358)
 * **nickhorn** and **markdw**, thanks for the “rand” tip. It’s exactly what my 
   client is asking me to do. I needed to randomly display the image posts of a 
   particular category. Image posts and where they will link are defined using custom
   fields.
 * Let me share to everyone as well the exact code I used. Thanks!
 *     ```
       <?php $recent = new WP_Query("cat=34&showposts=3&orderby=rand"); while($recent->have_posts()) : $recent->the_post();?>
   
       	<div class="ad_box">
       		<a href="<?php $values = get_post_custom_values("bigad link"); echo $values[0]; ?>" rel="bookmark"><img src="<?php bloginfo('stylesheet_directory'); ?>/bigads/<?php $values = get_post_custom_values("bigad image"); echo $values[0]; ?>" alt="" /></a>
       	</div>
       ```
   
 *  [displaced2000](https://wordpress.org/support/users/displaced2000/)
 * (@displaced2000)
 * [17 years, 9 months ago](https://wordpress.org/support/topic/show-random-posts-custom-fields/#post-749384)
 * This is just what i needed but the problem i have is not all of my post will 
   have the specific custum field.
 * Can i miss out the post that dont have this custom field and use the ones that
   do.
 * Thanks!
 *  [mjchecko](https://wordpress.org/support/users/mjchecko/)
 * (@mjchecko)
 * [17 years, 3 months ago](https://wordpress.org/support/topic/show-random-posts-custom-fields/#post-749410)
 * Just jumping in on markdw’s post: didn’t work for me but when I swapped out `
   orderby=RAND()` for `orderby=rand` it worked perfectly. Cheers to this thread!

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Show Random Posts Custom Fields’ is closed to new replies.

 * 10 replies
 * 5 participants
 * Last reply from: [mjchecko](https://wordpress.org/support/users/mjchecko/)
 * Last activity: [17 years, 3 months ago](https://wordpress.org/support/topic/show-random-posts-custom-fields/#post-749410)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
