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?

1
1

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.

Leave a Reply

Your email address will not be published. Required fields are marked *