About Lars Koudal

9 Comments

This is actually not the best method for this. Much better is to use WordPress function:

$postdata = get_post($postid);

This function uses internal WordPress object cache, and if the post you need is already in the cache you save on running unneeded query. Object is the same as with your method and all returned properties the same. .-= Milan Petrovic´s last blog ..New website res4WP.com =-.

Thank you for that function! That does indeed make it a lot easier, and as you say, if its already in the cache it is much faster. In cases where a lot of posts could be queried it would be good.

One note however, if in cases where you are only interested in for instance the title of a range of posts, would it not be faster to use my original query solution, modified to only return the title from the database, or would get_post() still be better/faster?

If the post is already cached it will be faster to use get_post, also get_post will cache post if it is not already. Also, such simple queries are fast do execute. But I try to avoid such coding because it is easier to do, but later is much more complicated to change and clean up.

Since I spend a lot of time optimizing GD Star Rating plugin and adding various caching methods, I have measured all kinds of things, and the conclusion is that the best thing is to have less DB queries. They are very fast, but take into the account total number of queries executed, and it’s best to find the way to minimize that. GD Star Rating pre 1.5 versions needed some 150 queries for 10 posts because it was executing queries as they were needed. Now it needs less then 10 queries for the same 10 posts.

cleverplugins.comon 04/11/2009 at 10:13

150 down to less than 10? That is what I call improvement! 🙂

Dave Lon 15/11/2009 at 08:08

I am sorry but what is this supposed to do? “outside the loop” of what exactly?

Thank you! I couldn’t believe the amount of time I had to spend to find such a simple solution. Was trying to limit the length of the title of the Previous and Next posts (without the need for plugins), since previous_post_link() and next_post_link() are so limited. The following should give anyone else looking for a solution enough of an idea.