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

Mike Schinkel mikeschinkel at newclarity.net
Tue May 5 08:21:31 GMT 2009


"Peter Westwood" <peter.westwood at ftwr.co.uk> wrote:
> The reason we recommend avoiding direct SQL is to reduce the 
> worry of  forward-compatibility for your theme or plugin.

With respect, I do understand your reasons and do believe that 95% of the time it is probably the right approach. But as with any guidelines there are times when it makes sense to bypass them, and that was my point.

> 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 required.

IF the query to find the most recent posts in each category is moved to a scheduled task or done on post update then I agree that would probably be one of the best solutions discussed albeit does require understanding how to implement the hooks which can be less than trivial (though not terribly hard.) 

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

Point of note, with the proper indexes the overhead of most subqueries and multi-join queries is minimal, especially as compared to 6 standard queries. Put another way, doing something in SQL will (almost) ALWAYS be much faster than doing it in PHP.

-Mike Schinkel
Custom Wordpress Plugins
http://mikeschinkel.com/custom-wordpress-plugins


More information about the wp-hackers mailing list