Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Show random post from custom post type WordPress

  • SOLVED

Hello I am using this bit of code to show a random post from the custom post type 'dish-of-the-day'
however it is showing 10 of the posts not just 1. Can someone correct my code?

<?php $rand_posts = query_posts('post_type=dish-of-the-day&numberposts=1&orderby=rand'); foreach( $rand_posts as $post ) : ?>
<div class="dishofthedayimage"><a href="<?php echo do_shortcode('[acf field="link_to_traders_page" post_id="'.get_the_ID().'"]'); ?>"><img src="<?php echo do_shortcode('[acf field="image_of_dish_of_the_day" post_id="'.get_the_ID().'"]'); ?>" width="100%" alt="<?php echo do_shortcode('[acf field="name_of_dish" post_id="'.get_the_ID().'"]'); ?>"/></a></div>
<div class="dishoftheday"><a href="<?php echo do_shortcode('[acf field="link_to_traders_page" post_id="'.get_the_ID().'"]'); ?>"><?php echo do_shortcode('[acf field="name_of_dish" post_id="'.get_the_ID().'"]'); ?></a></div>
<div class="sidebarhomefoodtitle"><?php the_title(); ?></div>
<?php endforeach; ?>

Answers (5)

2014-06-19

Arnav Joy answers:

try this

<?php $rand_posts = query_posts('post_type=dish-of-the-day&posts_per_page=1&orderby=rand'); foreach( $rand_posts as $post ) : ?>

<div class="dishofthedayimage"><a href="<?php echo do_shortcode('[acf field="link_to_traders_page" post_id="'.get_the_ID().'"]'); ?>"><img src="<?php echo do_shortcode('[acf field="image_of_dish_of_the_day" post_id="'.get_the_ID().'"]'); ?>" width="100%" alt="<?php echo do_shortcode('[acf field="name_of_dish" post_id="'.get_the_ID().'"]'); ?>"/></a></div>

<div class="dishoftheday"><a href="<?php echo do_shortcode('[acf field="link_to_traders_page" post_id="'.get_the_ID().'"]'); ?>"><?php echo do_shortcode('[acf field="name_of_dish" post_id="'.get_the_ID().'"]'); ?></a></div>

<div class="sidebarhomefoodtitle"><?php the_title(); ?></div>

<?php endforeach; ?>

2014-06-19

John Cotton answers:

query_posts('post_type=dish-of-the-day&orderby=rand&posts_per_page=1')


John Cotton comments:

query_posts('post_type=dish-of-the-day&orderby=rand&posts_per_page=1')

2014-06-19

Kyle answers:

Try this, it is posts_per_page for that var.

<?php $rand_posts = query_posts('post_type=dish-of-the-day&posts_per_page=1&orderby=rand'); foreach( $rand_posts as $post ) : ?>

<div class="dishofthedayimage"><a href="<?php echo do_shortcode('[acf field="link_to_traders_page" post_id="'.get_the_ID().'"]'); ?>"><img src="<?php echo do_shortcode('[acf field="image_of_dish_of_the_day" post_id="'.get_the_ID().'"]'); ?>" width="100%" alt="<?php echo do_shortcode('[acf field="name_of_dish" post_id="'.get_the_ID().'"]'); ?>"/></a></div>

<div class="dishoftheday"><a href="<?php echo do_shortcode('[acf field="link_to_traders_page" post_id="'.get_the_ID().'"]'); ?>"><?php echo do_shortcode('[acf field="name_of_dish" post_id="'.get_the_ID().'"]'); ?></a></div>

<div class="sidebarhomefoodtitle"><?php the_title(); ?></div>

<?php endforeach; ?>

2014-06-19

Hariprasad Vijayan answers:

Hi,

Try this


<?php $rand_posts = query_posts('post_type=dish-of-the-day&posts_per_page=1&orderby=rand'); foreach( $rand_posts as $post ) : ?>

<div class="dishofthedayimage"><a href="<?php echo do_shortcode('[acf field="link_to_traders_page" post_id="'.get_the_ID().'"]'); ?>"><img src="<?php echo do_shortcode('[acf field="image_of_dish_of_the_day" post_id="'.get_the_ID().'"]'); ?>" width="100%" alt="<?php echo do_shortcode('[acf field="name_of_dish" post_id="'.get_the_ID().'"]'); ?>"/></a></div>

<div class="dishoftheday"><a href="<?php echo do_shortcode('[acf field="link_to_traders_page" post_id="'.get_the_ID().'"]'); ?>"><?php echo do_shortcode('[acf field="name_of_dish" post_id="'.get_the_ID().'"]'); ?></a></div>

<div class="sidebarhomefoodtitle"><?php the_title(); ?></div>

<?php endforeach; ?>

2014-06-19

Navjot Singh answers:

Try this

<?php $rand_posts = query_posts('post_type=dish-of-the-day&posts_per_page=1&orderby=rand'); foreach( $rand_posts as $post ) : ?>

<div class="dishofthedayimage"><a href="<?php echo do_shortcode('[acf field="link_to_traders_page" post_id="'.get_the_ID().'"]'); ?>"><img src="<?php echo do_shortcode('[acf field="image_of_dish_of_the_day" post_id="'.get_the_ID().'"]'); ?>" width="100%" alt="<?php echo do_shortcode('[acf field="name_of_dish" post_id="'.get_the_ID().'"]'); ?>"/></a></div>

<div class="dishoftheday"><a href="<?php echo do_shortcode('[acf field="link_to_traders_page" post_id="'.get_the_ID().'"]'); ?>"><?php echo do_shortcode('[acf field="name_of_dish" post_id="'.get_the_ID().'"]'); ?></a></div>

<div class="sidebarhomefoodtitle"><?php the_title(); ?></div>

<?php endforeach; ?>