插件开发文档

管理文章元数据

💡 云策文档标注

概述

本文档介绍了在 WordPress 中管理文章元数据(metadata)的核心方法,包括添加、更新、删除元数据,以及处理字符转义和隐藏自定义字段的注意事项。

关键要点

  • 使用 add_post_meta() 添加元数据,需指定 post_id、meta_key、meta_value 和 unique 标志。
  • 使用 update_post_meta() 更新元数据,若键不存在则自动创建,支持可选 prev_value 参数。
  • 使用 delete_post_meta() 删除元数据,可指定 meta_value 进行精确删除。
  • 元数据值存储时会经过 stripslashes() 处理,需注意转义字符问题,可使用 wp_slash() 进行补偿。
  • 元数据键以 "_" 开头或值为数组时,不会在文章编辑屏幕或 the_meta() 中显示,可用于隐藏自定义字段。

代码示例

// 添加元数据示例
add_post_meta( 68, '_color', 'red', true );

// 更新元数据示例
$escaped_json = '{"key":"value with "escaped quotes""}';
update_post_meta( $id, 'double_escaped_json', wp_slash( $escaped_json ) );

注意事项

  • meta_key 应使用插件或主题相关前缀以提高可读性,如 wporg_featured_menu。
  • meta_value 可以是字符串、整数或数组,数组会自动序列化存储。
  • unique 标志控制键是否唯一,非唯一键可存储多个变体(如价格)。
  • 更新元数据时,若不提供 prev_value,默认更新所有匹配条目。
  • 处理 JSON 等包含转义字符的值时,需额外使用 wp_slash() 避免 stripslashes() 导致的解析错误。

📄 原文内容

Adding Metadata

Adding metadata can be done quite easily with add_post_meta() . The function accepts a post_id, a meta_key, a meta_value, and a unique flag.

The meta_key is how your plugin will reference the meta value elsewhere in your code. Something like mycrazymetakeyname would work, however a prefix related to your plugin or theme followed by a description of the key would be more useful. wporg_featured_menu might be a good one. It should be noted that the same meta_key may be used multiple times to store variations of the metadata (see the unique flag below).

The meta_value can be a string, integer, or an array. If it’s an array, it will be automatically serialized before being stored in the database.

The unique flag allows you to declare whether this key should be unique. A non unique key is something a post can have multiple variations of, like price.
If you only ever want one price for a post, you should flag it unique and the meta_key will have one value only.

Updating Metadata

If a key already exists and you want to update it, use update_post_meta() . If you use this function and the key does NOT exist, then it will create it, as if you’d used add_post_meta() .

Similar to add_post_meta() , the function accepts a post_id, a meta_key, and meta_value. It also accepts an optional prev_value – which, if specified, will cause the function to only update existing metadata entries with this value. If it isn’t provided, the function defaults to updating all entries.

Deleting Metadata

delete_post_meta() takes a post_id, a meta_key, and optionally meta_value. It does exactly what the name suggests.

Character Escaping

Post meta values are passed through the stripslashes() function upon being stored, so you will need to be careful when passing in values (such as JSON) that might 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""}
*/

Workaround

By adding one more level of escaping using the function wp_slash() (introduced in WP 3.6), you can compensate for the call to stripslashes():

$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""}
*/

Hidden Custom Fields

If you are a plugin or theme developer and you are planning to use custom fields to store parameters, it is important to note that WordPress will not show custom fields which have meta_key starting with an “_” (underscore) in the custom fields list on the post edit screen or when using the the_meta() template function.

This can be useful in order to show these custom fields in an unusual way by using the add_meta_box() function.

The example below will add a unique custom field with the meta_key name ‘_color’ and the meta_value of ‘red’ but this custom field will not display in the post edit screen:

add_post_meta( 68, '_color', 'red', true );

Hidden Arrays

In addition, if the meta_value is an array, it will not be displayed on the page edit screen, even if you don’t prefix the meta_key name with an underscore.