通用API文档

💡 云策文档标注

概述

Metadata API 是 WordPress 中用于检索和操作各种对象类型元数据的标准化接口,基于键值对表示,支持同一键对应多个值。

关键要点

  • Metadata API 提供 add_metadata()、delete_metadata()、get_metadata() 和 update_metadata() 等核心函数。
  • 默认元数据表包括 wp_commentmeta、wp_postmeta 和 wp_usermeta,分别存储评论、文章/页面和用户的元数据。
  • 自定义元类型需手动创建数据库表,结构必须包含 meta_id、object_id、meta_key 和 meta_value 四列。
  • Metadata API 的源文件位于 wp-includes/meta.php。

注意事项

使用自定义元类型时,需确保对应的 MySQL 表已存在,否则函数可能无法正常工作。


📄 原文内容

Overview

The Metadata API is a simple and standarized way for retrieving and manipulating metadata of various WordPress object types.

Metadata for an object is a represented by a simple key-value pair.

Objects may contain multiple metadata entries that share the same key and differ only in their value.

Function Reference

Add/Delete Metadata:

Get/Update Metadata:

Database Requirements

This function assumes that a dedicated MySQL table exists for the $meta_type you specify. Some desired $meta_types do not come with pre-installed WordPress tables, and so they must be created manually.

Default Meta Tables

Assuming a prefix of wp_, WordPress’s included meta tables are:

  • wp_commentmeta: Metadata for specific comments.
  • wp_postmeta: Metadata for pages, posts, and all other post types.
  • wp_usermeta: Metadata for users.

Meta Table Structure

To store data for meta types not included in the above table list, a new table needs to be created. All meta tables require four columns.

  • meta_id – BIGINT(20): unsigned, auto_increment, not null, primary key.
  • object_id – BIGINT(20): unsigned, not null.
    Replace object with the singular name of the content type being used.
    For instance, this column might be named post_id or term_id.
    Although this column is used like a foreign key, it should not be defined as one.
  • meta_key – VARCHAR(255): The key of your custom meta data.
  • meta_value – LONGTEXT: The value of your custom meta data.

Source File

Metadata API is located in <a href="https://core.trac.wordpress.org/browser/tags/5.2.1/src/wp-includes/meta.php#L0">wp-includes/meta.php</a>.

Related

Metadata APIadd_metadata()get_metadata()update_metadata()delete_metadata().