Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP_Query orderby 'rand' not working

Trying to order some posts I'm displaying on a single custom post type page with random, but they aren't random at all. :/

<?php 
// Grab the taxonomy term slug for the current post 
$terms = get_the_terms( get_the_ID(), 'category-staff' );

if ( $terms && ! is_wp_error( $terms ) ) : 

    $draught_links = array();

    foreach ( $terms as $term ) {
        $draught_links[] = $term->slug;
    }

    $on_draught = join( ", ", $draught_links );
    ?>

    <div class="container hidden-xs">
        <div class="row">
            <div class="col-sm-12">
                <hr />
                <h3 class="text-center">Other People At Our Great Resort</h3>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12 col-lg-10 col-lg-offset-1">
                <div class="row staff-list">
                    <?php
                        // WP_Query arguments
                        $args2 = array (
                            'post_type'              => 'staff',
                            'tax_query'              => array(
                                array(
                                    'taxonomy' => 'category-staff',
                                    'field'    => 'slug',
                                    'terms'    => $on_draught,
                                ),
                            ),
                            'nopaging'               => false,
                            'posts_per_page'         => '4',
                            'order'                  => 'DESC',
                            'orderby'                => 'rand',
                        );


                        // The Query
                        $query2 = new WP_Query( $args2 );

                        // The Loop
                        if ( $query2->have_posts() ) {
                            while ( $query2->have_posts() ) {
                                $query2->the_post(); ?>

                                <div class="staff staff-other col-sm-3 text-center">
                                    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                                        <?php echo get_the_post_thumbnail( $_post->ID, 'large', array( 'class' => 'img-responsive img-circle img-staff' ) ); ?>
                                        <h4><?php the_title(); ?></h4>
                                        <?php if (get_field('staff_job')) { ?>
                                            <p><?php the_field('staff_job'); ?></p>
                                        <?php } ?>
                                    </a>
                                </div>

                            <?php }
                        } else { ?>

                        <?php } 

                        // Restore original Post Data
                        wp_reset_postdata(); ?>

                </div>
            </div>
        </div>
    </div>


<?php endif; // terms if statement ?>
like image 725
Klikster Avatar asked Nov 16 '25 14:11

Klikster


2 Answers

Turns out it was something to do with WPEngine. They disable rand() from the server and it needs to be enabled manually.

like image 104
Klikster Avatar answered Nov 18 '25 06:11

Klikster


Another solution may be to add this code before running the new WP_Query($args) function.

remove_all_filters('posts_orderby');

https://developer.wordpress.org/reference/functions/remove_all_filters/

like image 44
Ghost Echo Avatar answered Nov 18 '25 04:11

Ghost Echo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!