WordPress 以任意顺序获取特定帖子
现在,我正在做:
$posts = get_posts(array('post_type' => 'page', 'post__in' => array(1, 3, 2, 9, 7)));
并且遇到两个问题:
- Post 3 的内容是
'post_type' => 'post'
,所以它没有被选中,但我想要它!如果我省略'post_type' =>; 'page'
,则仅选择帖子 3(因为它必须假定 'post_type' => 'post'。)。 我希望能够按帖子的 ID 任意排序 。如果我知道如何使用 MySQL,我可以这样做:
从 wp_posts 中选择 *,其中 ID IN (1, 3, 2, 9, 7) ORDER BY FIND_IN_SET(ID, '1,3,2,9,7');
但是,我应该如何使用 WordPress 来做到这一点?
Right now, I'm doing:
$posts = get_posts(array('post_type' => 'page', 'post__in' => array(1, 3, 2, 9, 7)));
and am having two issues:
- Post 3 is of
'post_type' => 'post'
, so it doesn't get selected, but I want it! If I leave out'post_type' => 'page'
, then only post 3 is selected (because it must assume 'post_type' => 'post'.). I want to be able to order the posts arbitrarily by their ids. If I knew how to use MySQL, I could do:
SELECT * FROM wp_posts WHERE ID IN (1, 3, 2, 9, 7) ORDER BY FIND_IN_SET(ID, '1,3,2,9,7');
But, how should I do this with WordPress?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先通过 ID 任意获取所有帖子,然后循环遍历所有帖子
您可以这样做:-
希望这会有所帮助
First fetch all posts arbitrarily by their ids and then loop through all the posts
You can do in this way:-
Hope this helps
#wordpress IRC 频道上的 Kawauso 告诉我“post_type 采用一组值”。由此,我发现以下内容也选择了帖子 3:
因此,我执行了以下操作:
Kawauso on the #wordpress IRC channel informed me that "post_type takes an array of values." From that, I found that the following also selects post 3:
So, I did the following:
根据此线程,您可以使用此代码,然后使用经典的 WordPress 循环:
According to this thread, you can use this code and then use the classic WordPress loop: