Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Let's say I've already got the $post object through some earlier process of looping through an array of IDs and using get_post() on each one of them, storing the resulting object in some array.

Later on, I want to loop through that array and do something with each post. I already have a $my_post object, so I could echo $my_post->post_title, or I could get fancy and echo apply_filters('the_title', $my_post->post_title, $my_post->ID) or I could just use the native get_the_title($my_post->ID).

Here's where my performance question comes in. How reliable is the native cache (I'm not talking about some crazy plugin, just the built-in wp_cache_get($post_id, 'posts') etc..)?

I guess what I'm asking is: what is the likelihood that get_the_title() will dip back into the DB and re-grab the post, as opposed to using the cache, and would I be better off from a performance perspective just using the object data that I already have? Clearly you get more sugar if you use the built-in stuff. But does it risk going back to the DB?

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Template tags rely on global $post (unless you explicitly provide something else to those that support it. So either:

  • they get something from that variable (no reason to go for it in database)

  • or they don't (then they fail because they have no clue what you want)

Under most normal circumstances there is no reason to worry about impact of template tags. That only comes into play if you start dealing with crazy amount of posts and/or apply overly complex filters.

Update after discussion in comments

get_post() always tries to invoke cache before it makes database call. Cache gets purged by functions that modify posts explicitly ( see clean_post_cache() ) or expires naturally.

share|improve this answer
    
Let's not oversimplify my question, Rarst. Actually, if you dig into the code, you see that template tags are significantly more sophisticated than that. At least the get_ variety. IF the global $post object has been defined, AND no ID has been supplied, then the global $post object is used; however, if the ID is supplied, then it goes to the cache. If the cache has nothing for that ID, then it goes to the database. –  Tom Auger Aug 26 '11 at 12:44
    
@Tom sorry if I missed on details, was late for me at the time. I am not sure I completely understand your concern. It's correct that if you have loaded post once with get_post() then subsequent calls for this post will go to cache. What do you think might go wrong here in context of your usage? –  Rarst Aug 26 '11 at 12:53
    
thanks for the follow-up. I think I just haven't dug deeply enough into wp_cache to understand how consistently something is cached. Is EVERY get_post() request cached for the session? Does the cache get purged after a certain limit? I believe that it's pretty safe to do get_the_permalink($id) and then get_the_title($id) and it will NOT be 2 separate database calls. Do you agree? –  Tom Auger Aug 26 '11 at 15:30
    
@Tom Auger yes, get_post() always tries to invoke cache before it makes database call. Cache gets purged by functions that modify posts explicitly ( see clean_post_cache() ) or expires naturally. –  Rarst Aug 26 '11 at 15:37
    
Awesome. Can you update your answer so I can accept it? –  Tom Auger Aug 26 '11 at 15:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.