0

I have a project that contains a button supposed to generate get a link to a random Custom Post item.

I made my Custom Query as follow:

<div class="content">
            <?php 

            $argsRandItem = array(
                'post_type' => 'participant',
                'posts_per_page' => 1,
                'orderby' => 'rand'
            );

            $queryRandItem = new WP_Query($argsRandItem);

            if ($queryRandItem->have_posts() ) : 
                while ( $queryRandItem->have_posts() ) : $queryRandItem->the_post();  ?>
                
                <a class="btn_blue" href="<?php the_permalink(); ?>">Discover a Project</a>
                <p><?php the_title(); ?></p>
                <hr>

               <?php
                    endwhile;  wp_reset_postdata();
                endif;  ?>
        </div>

The most strange thing is that the solution is working well on my local environment, and even on a pre-prod server as well. However, when the website is put online Live the button is always returning as a result a link to 1 specific post (Coincidentally - or not - the first one showing on the list of this Custom Post type).

I cannot imagine what could be a reason for this to be happening. The live version has enough items posted. Is it a normal bug on random Custom Post Type elements?

4
  • it is not a normal bug, WP wouldn't treat your CPT differently from others. I will note that rand is extremely heavy/slow/expensive, and the query can't be cached. Literally any other approximation will give better performance than this as the database needs to copy the table in memory, shuffle the rows, then perform the query. This code is also fundamentally incompatible with page caching and CDNs as the first time its loaded the result is cached and the next time someone visits it will serve the cached version from the first time.
    – Tom J Nowell
    Aug 9 at 20:59
  • @TomJNowell I cannot really think about any other solution. Any suggestion? I am pretty beginner on Wordpress. Thanks for your comment! Aug 10 at 1:28
  • By the way @TomJNowell, I just have about 150 items for this CPT. Aug 10 at 1:31
  • you can never cache the pages that use this query, be it via a plugin or CDN or service, e.g. cloudflare or WP Rocket. Any approximation will do, e.g. choosing a random date then asking for the first post that appears after that date. Random numbers in PHP are fast, random things in MySQL are slow. The other side is scaling, e.g. how many times can you run the query at the same time for concurrent visitors.
    – Tom J Nowell
    Aug 10 at 8:45
0

My guess is that your results are cached on the production environment.

Try bypassing cache by adding this to your query args:

'cache_results'  => false,

See https://developer.wordpress.org/reference/classes/wp_query/#caching-parameters

1
  • Hello @vancoder, thanks for your suggestion. I will push this and request a test on Live. Hope that it works! It's just so strange that it works perfectly on Local and pre-prod server but not on live version. Aug 10 at 1:29
0

I just realized what happened. On live version website - That I had access later - They were using WP Engine, and one of the advanced configurations are "Allow ORDER BY RAND()", which was disabled. Just making it enabled solved the issue.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.