WP_Query orderby'rand'不起作用

时间:2016-03-05 00:00:30

标签: php jquery random wp-query

尝试订购一些我随机发布在一个自定义帖子类型页面上的帖子,但它们根本不是随机的。 :/

<?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 ?>

2 个答案:

答案 0 :(得分:7)

原来这与WPEngine有关。他们从服务器禁用rand(),它必须是enabled manually

答案 1 :(得分:0)

另一种解决方案可能是在运行new WP_Query($args)函数之前添加此代码。

remove_all_filters('posts_orderby');

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