I am working on a project that requires some advanced ordering of posts and advanced pagination.

Basically, we have posts in a category, lets call this category 'mycategory'. We then have post priorities (standard, sub and main) (these priorities are stored as a custom post field value). Each page is paginated to show a total of 20 posts.

When a category page is displayed, the first page will display the following:

  • All posts are from the 'mycategory' category.
  • 1 post from the main priority is displayed at the top. (The latest one.)
  • 2 posts from the sub priority are displayed just below. (The 2 latest ones.)
  • The rest of the posts shown (17 posts) are a mix of standard posts, and any posts that are marked as a higher priority (main or sub) that have not been displayed above. In date order. Latest First.

So the first page should display the latest main, the 2 latest sub, and then any combination of main, sub, and standard that have not already been shown in the 3 above, in date order.

When the 2nd page is displayed, it works differently:

  • All posts are from the 'mycategory' category. as above
  • All other posts displayed are a combination of main, sub and standard that have not been displayed on the first page. Also in date order. Latest first.
  • The slots for the single main, and 2 sub, will be replaced with the other posts.

So the 2nd (and 3rd, and so on.) pages will display 20 posts (combination of standard, sub and main) in date order, that have not been shown on any previous page. This means that on page 2, if a post with a priority of sub is not displayed on the home page, it could display on the 2nd page (or where ever, based on date.) in a main slot, standard slot or in a sub slot.

I'm not too sure on how to get this to work in WordPress. Does anyone have any ideas at all?

Let me know if any more information is required.

1 Answer 1


Some thoughts on how I would approach this. I can update later with some code if necessary when I have the time.

I would handle the first page with two additional queries, so three loops on page one. Hook pre_get_posts and do two meta queries for the three posts that will appear in the first two loops. Those will be cached for later when you display them on your page. What you want now though is the post IDs. Use those to set post__not_in on your main query. Maybe put those IDs in a transient for subsequent pages? You'll have to exclude those 3 IDs from all pages for the pagination to be correct. Not sure on that one.

Page two and onward have the three top IDs excluded via the same post__not_in like you set on the first page.

To get 17 posts on page one and 20 posts on subsequent pages in the main query, you'll have to set offset manually, and correct found_posts via a filter. I just answered another question with some code that may be helpful here: Trying to exclude first 5 posts from the first page on the homepage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.