函数文档

has_meta()

💡 云策文档标注

概述

has_meta() 函数用于根据给定的文章 ID 返回相关的元数据。它通过查询数据库中的 postmeta 表来获取元数据信息,并以数组形式返回结果。

关键要点

  • 参数:接受一个必需的整数类型 post_id,表示要查询的文章 ID。
  • 返回值:返回一个数组,包含多个关联数组,每个关联数组代表一条元数据记录,包括 meta_key、meta_value、meta_id 和 post_id 字段。
  • 函数实现:使用全局 $wpdb 对象执行 SQL 查询,通过 wpdb::prepare() 进行安全查询,并按 meta_key 和 meta_id 排序。
  • 相关函数:依赖于 wpdb::get_results() 和 wpdb::prepare(),并被 post_custom_meta_box() 和 wp_xmlrpc_server::get_custom_fields() 使用。
  • 版本历史:自 WordPress 1.2.0 版本引入。

代码示例

function has_meta( $post_id ) {
    global $wpdb;

    return $wpdb->get_results(
        $wpdb->prepare(
            "SELECT meta_key, meta_value, meta_id, post_id
            FROM $wpdb->postmeta WHERE post_id = %d
            ORDER BY meta_key,meta_id",
            $post_id
        ),
        ARRAY_A
    );
}

📄 原文内容

Returns meta data for the given post ID.

Parameters

$post_idintrequired
A post ID.

Return

array[] Array of meta data arrays for the given post ID.
  • ...$0 array
    Associative array of meta data.
    • meta_key string
      Meta key.
    • meta_value mixed
      Meta value.
    • meta_id string
      Meta ID as a numeric string.
    • post_id string
      Post ID as a numeric string.

Source

function has_meta( $post_id ) {
	global $wpdb;

	return $wpdb->get_results(
		$wpdb->prepare(
			"SELECT meta_key, meta_value, meta_id, post_id
			FROM $wpdb->postmeta WHERE post_id = %d
			ORDER BY meta_key,meta_id",
			$post_id
		),
		ARRAY_A
	);
}

Changelog

VersionDescription
1.2.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.