WP_QUERY每次返回POSTS_PER_PAGE+1

时间:2016-08-19 作者:Jake Wilson

我有一个简单的WP_Query 我打算只返回最新的4篇帖子:

<?php
      $query = new WP_Query([
      \'post_status\' => \'publish\',
      \'orderby\' => \'date\',
      \'order\' => \'DESC\',
      \'posts_per_page\' => 4,
    ]);

    var_dump(count($query->posts));die;    // Returns 5 instead of 4
?>
count($query->posts) 每次为5。为什么不是4号?

1 个回复
最合适的回答,由SO网友:Nate Allen 整理而成

我见过当你有粘性帖子时会发生这种情况。尝试排除它们,如下所示:

<?php
      $query = new WP_Query([
          \'post_status\' => \'publish\',
          \'orderby\' => \'date\',
          \'order\' => \'DESC\',
          \'posts_per_page\' => 4,
          \'ignore_sticky_posts\' => true,
    ]);
?>