WP_Query orderby'rand'不起作用 [英] WP_Query orderby 'rand' not working

查看:76
本文介绍了WP_Query orderby'rand'不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

原来,这与WPEngine有关.他们从服务器禁用了rand(),需要手动启用.

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

这篇关于WP_Query orderby'rand'不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆