0

I've had multiple requests from different clients to figure out a way to modify their blog (index.php) or Custom Post Type Archive page in order to show less posts from a certain category.

A good example would be a client that has a system of automatic posting from social media (Facebook, Twitter, Instagram), which posts to their WordPress blog in the "Social Media" category. If this client posts a lot to social media, and writes actual posts on their blog sparingly, the social media posts will dominate their blog (at least on the first page). In this case, the client needs a way to limit the posts in the "Social Media" category per page, on the blog.

I looked into using pre_get_posts to modify the query, but can't seem to find any examples of how to limit the number of posts per page of a specific category, on the index.php page.

I was also considering making 2 separate queries (one for the "Social Media" category and another excluding this category) and combining them in a certain order. With this method, I was thinking that I could create a loop that combined the ID's of the 2 arrays (into 1), so that every fourth item would be from the "Social Media" category.

This sounds like a bit of hacking to me though, and I would like to find out if there is a more elegant/less messy way to get this done.

  • Does this help? – eyoung100 Jun 3 '14 at 16:35
  • @ECarterYoung the link you've provided actually has no value, sad to say. query_posts should not be used. Check out this post to understand why query_posts should not be used. And it not just us that say that, even the codex stated that it should not be used. Hope this helps you going forward – Pieter Goosen Jun 3 '14 at 17:48
  • "This sounds like a bit of hacking to me though" Please explain why you say this. – Pieter Goosen Jun 3 '14 at 17:54
  • @ECarterYoung That isn't exactly what I'm trying to do. I realize the article is not up to snuff, but it is not what I need either. Just excluding a category from the query would be very simple, and would exclude all of the posts from the "Social Media" category. I'm not trying to exclude them, just trying to limit the number of them that appear, per page. – seansean11 Jun 3 '14 at 21:10
0

The problem with this, as it often is, is not just homepage, but the continuous nature of pagination.

Let's say we start with 10 posts on home page and 5 of them social. Client says that they would like 2 less of social.

We can get rid of them easily enough, but now we need to "borrow" two more posts from second page. So we do that.

But next user clicks to second page. Instead of straightforward "next 10 posts" our logic is now "10 posts, after first 10, but displaying social posts we didn't display there and not displaying normal posts, which first page took from this one". Along these lines.

Next user clicks to page three... It compounds.

The only sane solution is two loops indeed. We leave normal posts and pagination up to main loop and we splice in anything extra in between (not necessarily modifying post array even, can just count and do something after every Nth post in loop), or displaying them separately as asides.

  • Very informative @Rarst! I'm going to give this a shot and post the code, if I can get it to work. – seansean11 Jun 3 '14 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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