If numberposts = -1 offset won’t work

I have an issue with a shortcode I have built. The short code pulls the most recent posts based off of certain parameters. below are the parameters

extract( shortcode_atts( array (
    'numberposts'   => 6,
    'offset'        => 0,
    'featured'      => null,
    'trending'      => null,
    'showdate'      => null
), $atts ) );

$args = array(
    'numberposts' => $numberposts,
    'offset' => $offset,
    'category__not_in' => array(391),
    'orderby' => 'post_date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish'
);

I am running this shortcode three times on the home page. The first time is like this:
(This one grabs the most recent 6 articles)

[recent-articles-grid featured="1" trending="1" showdate="1"]

The second time like this:
(This one grabs the next 4 articles)

[recent-articles-grid numberposts="4" offset="6" showdate="1"]

and the third time like this:
(This one is supposed to grab the remaining articles minus the first 10)

[recent-articles-grid numberposts="-1" offset="10"]

I have discovered that when I use -1 for the numberposts parameter it disregards the offset parameter. if I were to change the numberposts to something like 100 the offset works.

Is there a way to grab the remaining posts and still use the offset?

1 Answer
1

This problem has pretty simple explanation 😉 All you need to do is to take a look at Codex page for WP_Query and read about offset parameter:

offset (int) – number of post to displace or pass over. Warning:
Setting the offset parameter overrides/ignores the paged parameter and
breaks pagination (Click here for a workaround). The ‘offset’
parameter is ignored when ‘posts_per_page’=>-1 (show all posts) is
used.

So I’m afraid there is no easy workaround for that. Setting posts_per_page tells WP that you want to see all posts. So adding offset to that equation doesn’t make much sense.

On the other hand, if you already have all posts queried, then you can easily do the offset part by yourself – just ignore N first posts (not ideal, but it will work).

And – based on numberposts you use these get_posts function? If so, then it’s even easier to ignore first N posts – just start your loop from N-th.

Leave a Comment