31
How to Manage WordPress Post Metadata
The term WordPress post metadata refers to data about data. It’s information about posts, users, comments, and terminology in the case of WordPress.
Because of the many-to-one relationship of metadata in WordPress, you have a lot of possibilities. You can have as many meta settings as you like and store almost anything in there.
With add_post_meta(), you can simply add information to your posts. A post_id, a meta_key, a meta_value, and a unique flag are all accepted by the function.
The meta_key is used by your plugin to refer to the meta value in other parts of your code. Something like mycrazymetakeyname would work, A prefix linked to your plugin or theme followed by a description of the key would work, but a prefix relevant to your plugin or theme followed by a description of the key would be more beneficial. wporg_featured_menu is a possibility.
It's worth noting that the same meta_key can be used several times to record different metadata variants (see the unique flag below). A string, integer, or array can be used as the meta_value. If the value is an array, it will be serialized automatically before being put in the database.
You can use the unique flag to specify whether or not this key should be unique. A non-unique key is one that may be found in numerous versions in a post, such as price.
If you just want one price for a post, mark it as unique, and the meta_key will only have one value.
If you wish to update a key that already exists, use update_post_meta(). If you call this function and the key does NOT exist, it will generate it for you, much like if you used add_post_meta().
The function receives a post_id, a meta_key, a meta_value, and a unique flag, same as add_post_meta().
A post_id, a meta_key, and possibly a meta_value are sent to delete_post_meta(). It does exactly what it says on the tin.
Because post meta values are saved using the stripslashes() function, you must be cautious when sending in values (such as JSON) that may include escaped characters.
Consider the JSON value {"key":"value with \"escaped quotes\""}:
$escaped_json = '{"key":"value with \"escaped quotes\""}';
update_post_meta( $id, 'escaped_json', $escaped_json );
$broken = get_post_meta( $id, 'escaped_json', true );
/*
$broken, after stripslashes(), ends up unparsable:
{"key":"value with "escaped quotes""}
*/
You may compensate for the call to stripslashes() by adding another level of escaping using the function wp_slash() (added in WP 3.6):
$escaped_json = '{"key":"value with \"escaped quotes\""}';
update_post_meta( $id, 'double_escaped_json', wp_slash( $escaped_json ) );
$fixed = get_post_meta( $id, 'double_escaped_json', true );
/*
$fixed, after stripslashes(), ends up as desired:
{"key":"value with \"escaped quotes\""}
*/
If you’re a plugin or theme developer and want to utilize custom fields to store parameters, keep in mind that custom fields with a meta_key that starts with a “_” (underscore) will not appear in the custom fields list on the post edit page or when using the the_meta() template function.
Using the add_meta_box() method, can be handy for displaying these custom fields in a unique way.
The following example will create a unique custom field with the meta_key name ‘_color’ and the meta_value ‘red,’ but it will not appear in the post edit screen:
add_post_meta( 68, '_color', 'red', true );
If you have any query visit to our website
31