WP doesn't show Array Custom Fields?

by Paul   Last Updated April 15, 2019 09:08 AM

I'm wondering why WordPress doesn't list PHP array() and any serialized data in Custom Fields in Admin Panel (for Pages, Posts etc.)? Only Custom Fields containing strings and numbers show up and can be edited by user manually.

Edit: Why aren't post meta values displayed if they are stored as a non-string value, meaning, stored as either arrays or a serialized value ?

Example: If a post has a meta key 'custom-meta' with a string value as 'yes', it is displayed in the meta box, but if it has an array value like array('value' => 'yes'), it is not displayed in the meta box.

Can I enable this?



Answers 3


There is no filter to change that behavior, you would have to replace the entire metabox.

On the other hand: I think there is no really simple way to show and to save those arrays.

Example for a fictive meta key 'foo':

array (
    0 => 2,
    'hello' => array (
        0 => 2,
        'hello' => 'world'
    )
)

Creating a default interface for such an array would be very hard. This metabox is for simple fields, it should be easy to use. And you cannot just present the serialized string: editing that would probably break it. So I think it is a compromise. Better than nothing, but not perfect.

fuxia
fuxia
October 19, 2012 18:31 PM

For those landing here, you could always just create a custom page template on a private page and then just throw something like this on the template page file:

/*Template Name: Test Post Meta */
$thisPostMeta = get_post_meta($yourPostId, 'meta_key');

//Check for type of value
if (is_array($thisPostMeta)) {
print_r($thisPostMeta); 
} else {
echo $thisPostMeta;
}

Note: untested.

Eckstein
Eckstein
August 21, 2016 15:59 PM

I think you can use serialize();

Example:

$value = array('value' => 'yes'); $new_value = serialize($value); add_post_meta($post_id,'meta_key',$new_value);

then,you can see the metabox in admin panel.

秋蕊李
秋蕊李
April 15, 2019 08:31 AM

Related Questions





array_map() for sanitizing $_POST

Updated June 15, 2019 03:08 AM