wordpress orderby=rand 不工作 [英] wordpress orderby=rand not working

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

问题描述

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

每次 array 都是完全相同的顺序.为什么我的帖子不是随机的?是因为我使用的是 custom post_type 吗?请指教.

Every time the array is the exact same order. Why aren't my posts randomizing? Is it because I'm using a custom post_type? Please advise.

** 编辑 **

与此同时,我正在以我知道的唯一方式(效率低下)执行此操作.无论如何,请发布一个比这个更好的答案(为了各地的开发者!)

In the meantime I'm doing this the only way I know how (inefficient). By all means please post an answer better than this one (for the sake of developers everywhere!)

$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();

推荐答案

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

Try removing filters which might be interfering with orderby query.

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

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

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