最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

posts - WP_Query offset argument does not work

programmeradmin1浏览0评论

I want to display all posts starting from a certain offset, my query is:

WP_Query( array(
    'posts_per_page'    => -1,
    'offset'            => 20,
    'cat'               => 5,
) );

This query shows all posts without starting from the offset value. Whys is that?

I want to display all posts starting from a certain offset, my query is:

WP_Query( array(
    'posts_per_page'    => -1,
    'offset'            => 20,
    'cat'               => 5,
) );

This query shows all posts without starting from the offset value. Whys is that?

Share Improve this question edited Mar 2, 2016 at 19:40 Howdy_McGee 20.9k24 gold badges91 silver badges177 bronze badges asked Mar 2, 2016 at 19:24 rapidpagerapidpage 1031 gold badge1 silver badge4 bronze badges 3
  • What happens if you do posts_per_page=200? – Howdy_McGee Commented Mar 2, 2016 at 19:26
  • @Howdy_McGee Works properly. But why not work with a value of -1 ? – rapidpage Commented Mar 2, 2016 at 19:31
  • See my answer, you may also try using nopaging => true or paged => 1 to see if that also works with offset and your -1 value. – Howdy_McGee Commented Mar 2, 2016 at 19:36
Add a comment  | 

1 Answer 1

Reset to default 28

The offset index for WP_Query generally works with pagination. When you set pagination to a -1 the function assumes you're getting all posts and there will be no pagination or offset. So to counteract this you would set the posts_per_page to a high number like 999. Reading the Function Reference on WP_Query the pagination section says:

'posts_per_page'=>-1 to show all posts (the 'offset' parameter is ignored with a -1 value). Set the ‘paged’ parameter if pagination is off after using this parameter.

发布评论

评论列表(0)

  1. 暂无评论