wordpress orderby = rand not working

时间:2012-08-01 16:09:10

标签: wordpress

$hpPromo = new WP_Query(array('post_type' => 'homepage_promo', 'orderby' => 'rand'));
print_r($hpPromo->posts);

每次array都是完全相同的订单。为什么我的帖子没有随机化?是因为我正在使用自定义 post_type吗?请指教。

**编辑**

与此同时,我这是我知道如何(效率低下)的唯一方式。无论如何,请发布一个比这个更好的答案(为了各地的开发人员!)

$hpPromo = new WP_Query(array('post_type' => 'homepage_promo', 'orderby' => 'rand'));
$count = count($hpPromo->posts);
$posts = (array)$hpPromo->posts;
$promo_id = $posts[rand(0, $count-1)]->ID;
wp_reset_query();
$hpPromo = new WP_Query(array('post_type' => 'homepage_promo', 'p' => $promo_id));
$hpPromo->the_post();
the_content();

1 个答案:

答案 0 :(得分:10)

尝试删除可能干扰orderby查询的过滤器。

remove_all_filters('posts_orderby');
$hpPromo = new WP_Query(array('post_type' => 'homepage_promo', 'orderby' => 'rand'));
print_r($hpPromo->posts);