【问题标题】:WP_Query orderby 'rand' not workingWP_Query orderby 'rand' 不起作用
【发布时间】:2016-06-18 21:47:37
【问题描述】:

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

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

【问题讨论】:

    标签: php jquery random wordpress


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      remove_all_filters('posts_orderby');
      

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

      【讨论】:

        猜你喜欢
        • 2017-02-08
        • 2016-06-15
        • 1970-01-01
        • 2012-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多