I have a function to instantiate a class to provide var access to other functions.
function my_data(){
global $post,$the_data;
$postid = get_the_ID();
$the_data = new MY_Class ( $postid );
return $the_data;
}
This function will be called by every post, and each post will call it many times whenever need a var from MY_Class.
The data is saved in WordPress postmeta and retrived in My_Class.
Does WP automatically cache the object, or should I find a way to cache it? How?
If you use the WordPress API to retrieve the metadata, then it should be cached for you.
If you do other complex stuff, there is the Transients API for caching data yourself, which will take advantage of whatever object cache you use with WordPress.
EDIT – I should clarify, it’ll be loaded for each request unless you use some sort of caching plugin, though it should load only once for each request.