前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WordPress 技巧:在 WP_Query 使用 post__in 的时候注意要 ignore_sticky_posts

WordPress 技巧:在 WP_Query 使用 post__in 的时候注意要 ignore_sticky_posts

作者头像
Denis
发布2023-04-15 10:21:31
2440
发布2023-04-15 10:21:31
举报
文章被收录于专栏:WordPress果酱WordPress果酱

今天和同事在使用 WP_Query 的 post__in 参数的时候:

代码语言:javascript
复制
$like_query = new WP_Query(array(
	'post_type'	=> array('post','event'),
	'post__in'	=> array(138,139),
	'orderby'	=> 'post__in',
	'posts_per_page'=> -1
) );

但是返回的结果总是超过这个 138, 139 这两篇,甚是奇怪。后面仔细查看文档,才发现有如下这段话:

ATTENTION If you use sticky posts, they will be included (prepended!) in the posts you retrieve whether you want it or not. To suppress this behaviour use ignore_sticky_posts.

原来如此,我又正好使用了 sticky posts,置顶文章,所以,哎,调试了整整好几个小时,都把 WP 源代码翻烂了。

所以最终的代码应该是:

代码语言:javascript
复制
$like_query = new WP_Query(array(
	'post_type'		=> array('post','event'),
	'post__in'		=> array(138,139),
	'orderby'		=> 'post__in',
	'posts_per_page'	=> -1,
	'ignore_sticky_posts'	=> 1
) );

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档