Wordpress: Use add_filter to alter meta data of a post

I wanted to alter the meta used by a post type before it is actually retrieved. This can be useful since a lot of plugins and themes use metadata to store information about posts.

function [your theme]_post_metadata($metadata, $object_id, $meta_key, $single) {
if ($meta_key == '[your meta key]') {
$meta_type = 'post';
$meta_cache = wp_cache_get($object_id, $meta_type . '_meta');
if ( !$meta_cache ) {
$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
$meta_cache = $meta_cache[$object_id];
}
if ( !$meta_key )
$metadata = $meta_cache;
if ( isset($meta_cache[$meta_key]) ) {
if ( $single )
$metadata = maybe_unserialize( $meta_cache[$meta_key][0] );
else
$metadata = array_map('maybe_unserialize', $meta_cache[$meta_key]);
}
// Do whatever you want with $metadata. You may need to unserialize it.
return metadata;
}
}
add_filter('get_post_metadata', '[your theme]_post_metadata', true, 4);