[wp-hackers] Pull Latest Post from Each of 6 Categories

Peter Westwood peter.westwood at ftwr.co.uk
Tue May 5 08:05:03 GMT 2009

On 5 May 2009, at 08:40, Mike Schinkel wrote:

> "Austin Matzko" <if.website at gmail.com> wrote:
>> I am arguing against using MySQL directly, not suggesting
>> that my way is better than what he's already doing.
> Fair point. OTOH I'm challenging the conventional wisdom that it is  
> always best to use the WordPress API. In most cases I agree that use  
> of the WordPress API is best, especially in plugins hosted on Extend  
> but I'm not convinced that it always makes more sense to use the  
> WPAPI instead of hardcoding SQL.

The reason we recommend avoiding direct SQL is to reduce the worry of  
forward-compatibility for your theme or plugin.

We always try and keep the API backward compatible, we may not keep  
the same db structure.

With respect to the problem at hand:

"Show the latest post from 6 different categories on the front page"

I would recommend either the current approach,

i.e. six WP_Query using loops which query for the required posts  

If this is too slow then move the heavy work into something which  
happens on admin pages, by this I mean something like the following:

Keep an option in the db which contains an array containing the six  
post ids you want to display and update this option when a new post is  
written in one of the categories.

Then you can as far as I remember do a single WP_Query loop with the  
post_ids specified using 'post__in' and keep to always using the API.

This moves any heavy code to the backend where it will run when  

Calling a hefty SQL query with subqueries or hairy JOINs is no going  
to be performant.


Peter Westwood
http://blog.ftwr.co.uk | http://westi.wordpress.com
C53C F8FC 8796 8508 88D6 C950 54F4 5DCD A834 01C5

More information about the wp-hackers mailing list