Avatar of igloobob
igloobobFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

WP_Query 'orderby' => 'rand' isn't working for a custom post type

Hello,

can anyone please advise why this is not working? It is just showing the same post each page refresh rather than a new random one from the list.

<?php $loop = new WP_Query( array(
'post_type' => 'testimonials',
'posts_per_page' => 1,
'orderby' => 'rand' ) );
?>

<?php while( $loop->have_posts() ) : $loop->the_post(); ?>

<div class="quote_bubble"><?php the_content(); ?></div>										

<div class="speech_bubble_credit"><?php the_field('testimonial_name'); ?><br />

<?php the_field('testimonial_job_company'); ?>, <?php the_field('testimonial_company'); ?></div>

<?php endwhile; wp_reset_query(); ?>

Open in new window


This is just displaying the first post I added to the site again and again every time page is refreshed too if that is of any use in trying to establish the problem.

Any ideas anyone please?

Thanks!
WordPressPHP

Avatar of undefined
Last Comment
igloobob
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

Where are you hosted? Some ISPs disable the rand function because it takes too many resources.
Avatar of igloobob
igloobob
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

hosted on WP Engine
ASKER CERTIFIED SOLUTION
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of igloobob
igloobob
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Aha! Thanks that's sorted it! Thought I was going mad, as I was sure this exact loop was working on the dev version of the site before migrating to WPE!

Thanks!
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo