get_metadata

函式
get_metadata ( $meta_type, $object_id, $meta_key = '', $single = false )
引數
  • (string) $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', or any other object type with an associated meta table.
    Required:
  • (int) $object_id ID of the object metadata is for.
    Required:
  • (string) $meta_key Optional. Metadata key. If not specified, retrieve all metadata for the specified object. Default empty.
    Required:
    Default: (empty)
  • (bool) $single Optional. If true, return only the first value of the specified `$meta_key`. This parameter has no effect if `$meta_key` is not specified. Default false.
    Required:
    Default: false
返回值
  • (mixed) An array of values if `$single` is false. The value of the meta field if `$single` is true. False for an invalid `$object_id` (non-numeric, zero, or negative value), or if `$meta_type` is not specified. An empty string if a valid but non-existing object ID is passed.
相關
  • get_metadata_raw()
  • get_metadata_default()
定義位置
相關方法
get_metadata_rawget_commentdatadelete_metadataget_theme_dataget_metadata_by_mid
引入
2.9.0
棄用
-

get_metadata函式用來檢索一個給定物件(例如一個文章、評論或使用者)的後設資料和後設資料鍵: 這個函式可以用來檢索已經與一個物件相關聯的自定義後設資料。

檢索指定物件型別和ID的後設資料欄位的值。

如果後設資料欄位存在,如果`$single`為真,則返回一個單一的值,如果為假,則返回一個陣列的值。

如果元欄位不存在,其結果取決於get_metadata_default()。預設情況下,如果`$single`為真,將返回一個空字串,如果為假,將返回一個空陣列。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) {
$value = get_metadata_raw( $meta_type, $object_id, $meta_key, $single );
if ( ! is_null( $value ) ) {
return $value;
}
return get_metadata_default( $meta_type, $object_id, $meta_key, $single );
}
function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) { $value = get_metadata_raw( $meta_type, $object_id, $meta_key, $single ); if ( ! is_null( $value ) ) { return $value; } return get_metadata_default( $meta_type, $object_id, $meta_key, $single ); }
function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) {
	$value = get_metadata_raw( $meta_type, $object_id, $meta_key, $single );
	if ( ! is_null( $value ) ) {
		return $value;
	}

	return get_metadata_default( $meta_type, $object_id, $meta_key, $single );
}

常見問題

FAQs
檢視更多 >