How to hook a filter to catch get_post_meta when alternate a custom field output?
I have fill a custom field(meta data) in a post, just like this:
<!--:de-->Nominale spanning<!--:--><!--:zh/cn-->é¢å®çµå<!--:--><!--:en-->Arrester Accessories<!--:-->
I need to get this output translated,so I wondering how to hook into “get_post_meta” before the meta data output.
Here is what I’ve tried for a few days, but no luck.
function getqtlangcustomfieldvalue($metadata, $object_id, $meta_key, $single){
$fieldtitle="fields_titles";
if($meta_key==$fieldtitle&& isset($meta_key)){
//here is the catch, but no value has been passed
}
}
//Specify 4 arguments for this filter in the last parameter.
add_filter('get_post_metadata', 'getqtlangcustomfieldvalue', 10, 4);
After a lot of messing around with this, I think I found a fairly good solution here. I realize this is over a year after you asked but this was bothering me and I couldn’t find a good solution until now.
The problem is that the get_post_metadata function doesn’t allow you to access the current value. This means you aren’t able to transform the value, just replace it. I needed to append content to a meta field and where it was output did not allow filters of any kind.
Here’s my solution, altered to fit what this question asks:
This will keep any other get_post_metadata filters intact and allow modification of the original value.
Just had the same problem and, using your code above, here is how I solved it:
I tried using apply_filters, get_metadata, get_post_meta directly within the function but they wouldn’t allow me to manipulate the resulting output, so I resorted to using $wpdb.
Here’s my solution for filtering post meta. This then calls a custom function to perform any required data manipulation.