我们都知道ignore_sticky_posts 用于从自定义查询中排除粘性帖子。
-不,这个假设是错误的
什么
ignore_sticky_posts 意思是:即使在自然英语中,
ignore_sticky_posts 听起来WordPress应该忽略查询中的所有粘性帖子,实际上WordPress并不是这样做的。相反,你应该阅读
\'ignore_sticky_posts\' => 1 论点如下:
什么时候ignore_sticky_posts 设置为true 或1, WordPress将ignore the procedure 在自定义查询中设置粘性帖子。
WordPress在ignore_sticky_posts 未设置:以清楚地了解\'ignore_sticky_posts\' => 1 你需要了解WordPress在ignore_sticky_posts 参数未设置或已设置为false 或0 (默认情况下):
如果查询结果中有帖子是贴子的一部分,WordPress会将它们推到查询结果的顶部。
如果查询结果中没有任何粘性帖子,WordPress将再次从数据库中获取所有这些粘性帖子,并将它们设置到查询结果的顶部。
因此,当参数设置为\'ignore_sticky_posts\' => 1, WordPress简单ignores the above procedure, 仅此而已。它并没有明确排除他们。为此,您需要设置post__not_in 论点
法典示例说明:现在,让我们来看法典中的示例:
$args = array(
\'posts_per_page\' => 1,
\'post__in\' => get_option( \'sticky_posts\' ),
\'ignore_sticky_posts\' => 1
);
$query = new WP_Query( $args );
此处codex是唯一设置
\'ignore_sticky_posts\' => 1 to be efficient, nothing more. 即使没有它,您也会得到相同的预期结果:
$args = array(
\'posts_per_page\' => 1,
\'post__in\' => get_option( \'sticky_posts\' )
);
$query = new WP_Query( $args );
然而,在这种情况下
\'ignore_sticky_posts\' => 1 参数未设置,WordPress将不必要地执行所有那些将粘性帖子设置到结果顶部的过程,即使所有这些结果(来自本例)都只是粘性帖子。
学习WordPress的最好方法是检查核心代码。因此,为了更清楚地理解,请检查this part of WordPress CODE.