Wordpress get_post_metadata() returns serialized array

1.3k views Asked by At

I'm using Wordpress and Calderawp forms to build a survey site and I've run into an issue with checkbox selection results.

I've used a Caldera processor to push each collected field in my survey form to a custom field within a custom post type. My issue is that get_post_metadata is returning a massive, serialized array that looks like a class object.

Here is a screenshot of the result of one of the custom fields I'm referring to: enter image description here

And, here is my current attempt to alter the output:

/** 
 * Alter output of custom fields 
 */
add_filter( 'get_post_metadata', 'custom_get_post_metadata_filter', 10, 4 );
function custom_get_post_metadata_filter( $value, $post_id, $meta_key, $single ){

    //This prevents infinite loop
    remove_filter( 'get_post_metadata', 'custom_get_post_metadata_filter', 10  );

    $value = get_post_meta( $post_id, $value, true );
    add_filter( 'get_post_metadata', 'custom_get_post_metadata_filter', 10, 4 );

    foreach ( $value as $key => $val ) {
        $unserialized = unserialize($val);
        echo 'key: ' . $key . '| val: ' . $unserialized . '<br/>';
    }

}

The result of this code is something like:
key: some_key| val:
key: another_key| val:
key: yet_another_key| val:
...

I also just tried to print the $val and the result is another Array

Additionally, I've tried to use multiple foreach loops with setting a new variable, such as:

$my_key = $val['some_key'];

foreach($my_key as $k => $v) {
    echo $v;
}

...and I've found that the nested arrays go layers and layers deep.

Has anyone experienced this issue and found a solution?

1

There are 1 answers

0
timk260 On

looks like the following line is using the wrong parameter for the 'key' argument required by the function:

$value = get_post_meta( $post_id, $value, true );

should be

$value = get_post_meta( $post_id, **$meta_key**, true );