I am not sure how WordPress cache the queries. I had the impression that whenever I execute a query through wpdb class, it gets cached.
For example, on codex under Select a Row and Select a Var, it says the whole query is cached for later use. And I think which means if more data is requested in another query, partials or full results of which are already in wpdb cache, then those are used and a query doesn’t happen(in case of full results already in cache).
Am I correct in understanding?
I was trying something and I found that I can’t use the cache.
For reference, I was listing the recent comments the current user has made. I used get_comments()
but since it only has post id in the results, I used get_the_title()
inside the loop for displaying them. Obviously this is expensive in terms of query, so I though I can cache the required rows of post table by querying them beforehand so that get_the_title()
does no actual query.
I did something like
$query = implode( ' OR ID = ', $collect_post_ids );
$query = 'SELECT * FROM '.$wpdb->prefix.'posts WHERE ID = '.$query.';';
$wpdb->get_results( $query ); // just cache it
but that didn’t help. get_the_title()
is still doing queries. Most likely its me who has misunderstood how WP Cache works. So where I am wrong?
Here is the full code for reference – http://ashfame.pastebin.com/BpxjHiQr