Issue with get_post_metadata and serialized data
-
I’m writing an extension for a plugin, the base plugin pulls in the data like this:
$ingredients = get_post_meta( $recipe->ID, "rpr_recipe_ingredients", true );In my extension plugin I want to make some modifications to the ingredients, so I added a filter for get_post_metadata
add_filter('get_post_metadata', array($this, 'get_recipe_custom_val'), 10, 4);And here’s the function
public function get_recipe_custom_val($metadata, $object_id, $meta_key = '', $single = false) { $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 ) return $meta_cache; if ( isset($meta_cache[$meta_key]) ) { if ( $single ) { //print_r(maybe_unserialize( $meta_cache[$meta_key][0])); return maybe_unserialize( $meta_cache[$meta_key][0]); } else { return array_map('maybe_unserialize', $meta_cache[$meta_key]); } } }If I print_r the maybe_unserialize() line, it shows all the data properly, but when it actually returns the data it just has the first item.
This code was taken directly from get_metadata, and when I turn off the filter that maybe_unserialize works fine and all the ingredients come through and list properly, it’s just when it goes through the filter and does the same thing that it only returns the first item.
I don’t want to change anything in the base plugin, so that get_post_meta can’t change, I just don’t have any clue why the value that gets stored in $ingredients is different than what I print_r immediately before returning.
Also, it turns from a two dimensional array like $ingrdients[0][‘notes’] to $ingredients[‘notes’], it doesn’t just drop the other items in the two dimensional array.
Any ideas?
-
Solved it, after realizing it removed a dimension I changed it to
return array(maybe_unserialize( $meta_cache[$meta_key][0]));so when whatever strips a dimension off of it does so it ends up back at 2 dimensions.
The topic ‘Issue with get_post_metadata and serialized data’ is closed to new replies.