Store JSON in a custom field

Yes! Yes you can!

You can use this code to retrieve a stored option:

$value = get_post_meta($post->ID,'customfieldname',true); // where true means only return 1 custom field by this name

You can use this code to save a stored option:

update_post_meta($post->ID,'customfieldname',$valuetobestored);

The above code will need to be inside the post loop, though you can supply a post ID from elsewhere, say a GET variable etc and avoid another query

You can also store multiple values with the same custom field name. The value to be stored is a string value, as such a json string is good. The same is true of get_option and update_option for global values.

You can also use meta boxes to customize the UI for showing these values. Custom fields whose names start with _ will not be shown in the custom fields meta box in the back end ( which is how wordpress hides its own internal custom fields from the end user )

Leave a Comment