I have 3 categories with each 15 posts, I want to do ONE query to the db bringing only 5 first posts for each category, how can I do it?
$q = new WP_Query(array( 'post__in' => array(2,4,8), 'posts_per_page' => **FIRST_5_OF_EACH_CAT** ));
In case its not possible, what is more efficient, getting all the posts for the parent category and looping through them or creating 3 different queries?
What you want is possible but will require you to delve into SQL which I like to avoid whenever possible (not because I don’t know it, I’m an advance SQL developer, but because in WordPress you want to use the API whenever possible to minimize future compatibility problems related to future potential database structure changes.)
SQL with a
UNION
Operator is a PossibilityTo use SQL what you need is a
UNION
operator in your query, something like this assuming your category slugs are"category-1"
,"category-1"
and"category-3"
:You can use SQL UNION with a
posts_join
FilterUsing the above you can either just make the call directly or you can use a
posts_join
filter hook like as follows; note I’m using a PHP heredoc so be sure theSQL;
is flush left. Also note I used a global var to allow you to define the categories outside of the hook by listing the category slugs in an array. You can put this code in a plugin or in your theme’sfunctions.php
file:But There Can Be Side-effects
Of course using the query modification hooks like
posts_join
always invites side-effects in that they act globally on queries and thus you usually need to wrap your modifications in anif
that only uses it when needed and what criteria to test for can be tricky.Focus on Optimization Instead?
However, I assume your question is concerned is more about optimization than about being able to do a top 5 time 3 query, right? If that is the case then maybe there are other options that use the WordPress API?
Better to Use Transients API for Caching?
I assume your posts won’t change that often, correct? What if you accept the three (3) query hit periodically and then cache the results using the Transients API? You’ll get maintainable code and great performance; a good bit better than the
UNION
query above because WordPress will store the lists of posts as a serialized array in one record of thewp_options
table.You can take the following example and drop into your web site’s root as
test.php
to test this out:Summary
While yes you can do what you asked for using a SQL
UNION
query and theposts_join
filter hook you are probably better offer using caching with the Transients API instead.Hope this helps?
WP_Query()
does not support something like First X For Each Cat. Instead ofWP_Query()
you can useget_posts()
on each of your categories, so to say three times:$posts
now contains the first five posts for each category.I don’t know of a way to get the first five posts for each of the categories in a single query. If you’re ever going to only have 45 posts, then hitting the database once and getting your five posts for each category is probably the most efficient approach. Hitting the database three times and combining the results isn’t a bad thing though.