Limit the number of posts from a specific category on index.php
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.