SYNCER

SYNCER

get_metadata() - 指定したオブジェクトのメタデータを取得する

公開日:

get_metadata()は、指定したオブジェクトとIDのコンテンツに紐付けられたカスタムフィールドなどのメタデータを取得する関数です。

構文

mixed get_metadata( string $meta_type, int $object_id, string $meta_key = '', bool $single = false )

パラメータ

$meta_type

オブジェクトの種類。投稿や固定ページだったらpost、ユーザーだったらuser、コメントだったらcommentなど。

$object_id

対象コンテンツのID。

$meta_key

初期値: ''

取得するカスタムフィールドのキー名。指定しない場合、全てのカスタムフィールドを取得する。

$single

初期値: false

通常、各カスタムフィールドの値は配列形式になっているが、第3引数でtrueを指定すると、文字列で返す。ケースによっては、取得した値をそのまま利用できる利点などがある。

返り値

mixed

キー名を指定した場合、そのキー名のメタデータを取得する。

第3引数でキー名を指定しなかった場合、オブジェクトに紐付けられている全てのメタデータを含んだ配列を取得する。

第4引数でtrueを指定した場合、値は配列ではなく文字列で返る。

関数

この関数内で利用している、別のWordPress関数です。

  • absint() - 数字を絶対値に変換する。
  • apply_filters() - フィルターイベントを発火する。
  • maybe_unserialize()
  • update_meta_cache()
  • wp_cache_get()

フック

関数内で呼び出されるイベントです。

フィルター

get_{$meta_type}_metadata

サンプルコード

投稿

投稿オブジェクト(post)の、IDが11(リンク)のコンテンツに紐付けられたメタデータを取得します。

php

<?php
	// 実行
	$result = get_metadata( "post", 11 ) ;

	// 結果
	print_r( $result ) ;

結果 (出力内容) - PHP7.0.13

Array
(
    [_edit_last] => Array
        (
            [0] => 1
        )

    [_edit_lock] => Array
        (
            [0] => 1481696435:1
        )

    [enclosure] => Array
        (
            [0] => https://wp.syncer.jp/wp-content/uploads/2016/12/cute-dog.mp4
244970
video/mp4

        )

)

ユーザー

IDが1のユーザー(user)に紐付けられたメタデータを取得します。

php

<?php
	// 実行
	$result = get_metadata( "user", 1 ) ;

	// 結果
	print_r( $result ) ;

結果 (出力内容) - PHP7.0.13

Array
(
    [nickname] => Array
        (
            [0] => syncer
        )

    [first_name] => Array
        (
            [0] => SYNCER
        )

    [last_name] => Array
        (
            [0] => 
        )

    [description] => Array
        (
            [0] => 
        )

    [rich_editing] => Array
        (
            [0] => true
        )

    [comment_shortcuts] => Array
        (
            [0] => false
        )

    [admin_color] => Array
        (
            [0] => fresh
        )

    [use_ssl] => Array
        (
            [0] => 0
        )

    [show_admin_bar_front] => Array
        (
            [0] => true
        )

    [locale] => Array
        (
            [0] => 
        )

    [wp0_capabilities] => Array
        (
            [0] => a:1:{s:13:"administrator";b:1;}
        )

    [wp0_user_level] => Array
        (
            [0] => 10
        )

    [dismissed_wp_pointers] => Array
        (
            [0] => 
        )

    [show_welcome_panel] => Array
        (
            [0] => 1
        )

    [session_tokens] => Array
        (
            [0] => a:6:{s:64:"247ab4315438ccec1724dc8640182622341e55e0ddb03de03abcaffd76861c3e";a:4:{s:10:"expiration";i:1482807078;s:2:"ip";s:9:"10.0.1.74";s:2:"ua";s:120:"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36";s:5:"login";i:1481597478;}s:64:"8ba94f429d3e08350a505281474f45ea0a6b55852f83690cfd717b3ce0ca5052";a:4:{s:10:"expiration";i:1481770920;s:2:"ip";s:9:"10.0.1.74";s:2:"ua";s:120:"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36";s:5:"login";i:1481598120;}s:64:"3e54f337c5b2e2aa44ca32a06b983c0ff1f1f398fb746a1d11b4aa712f6f5a62";a:4:{s:10:"expiration";i:1481771517;s:2:"ip";s:9:"10.0.1.74";s:2:"ua";s:120:"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36";s:5:"login";i:1481598717;}s:64:"e4dfd8f7ea9660a95053e6e886a34f7e404f190386fe76b8393191ac969483d1";a:4:{s:10:"expiration";i:1481772255;s:2:"ip";s:10:"10.0.0.206";s:2:"ua";s:120:"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36";s:5:"login";i:1481599455;}s:64:"39e0e1a625f38edc96612cebf49ffff13b82f9000289afa742af3c6cf8969c66";a:4:{s:10:"expiration";i:1481772626;s:2:"ip";s:10:"10.0.0.206";s:2:"ua";s:120:"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36";s:5:"login";i:1481599826;}s:64:"822078200629a631e8dc52e6c4079e59345cf3f8a2dfec55106922ead51d1052";a:4:{s:10:"expiration";i:1481773241;s:2:"ip";s:10:"10.0.0.206";s:2:"ua";s:120:"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36";s:5:"login";i:1481600441;}}
        )

    [wp0_dashboard_quick_press_last_post_id] => Array
        (
            [0] => 3
        )

    [closedpostboxes_post] => Array
        (
            [0] => a:0:{}
        )

    [metaboxhidden_post] => Array
        (
            [0] => a:0:{}
        )

    [wp0_user-settings] => Array
        (
            [0] => editor=html&libraryContent=browse&align=center&urlbutton=post
        )

    [wp0_user-settings-time] => Array
        (
            [0] => 1481602970
        )

    [closedpostboxes_attachment] => Array
        (
            [0] => a:0:{}
        )

    [metaboxhidden_attachment] => Array
        (
            [0] => a:0:{}
        )

    [closedpostboxes_page] => Array
        (
            [0] => a:0:{}
        )

    [metaboxhidden_page] => Array
        (
            [0] => a:0:{}
        )

)

特定のメタデータ

第3引数でキー名を指定することで、特定のメタデータだけを取得できます。

php

<?php
	// 実行
	$result = get_metadata( "user", 1, "nickname" ) ;

	// 結果
	print_r( $result ) ;

結果 (出力内容) - PHP7.0.13

Array
(
    [0] => syncer
)

文字列で取得

先ほどと同じメタデータですが、第4引数にtrueを指定することで、配列ではなく文字列で値を取得します。

php

<?php
	// 実行
	$result = get_metadata( "user", 1, "nickname", true ) ;

	// 結果
	print_r( $result ) ;

結果 (出力内容) - PHP7.0.13

syncer

ソースコード

wp-includes/meta.php

/**
 * Retrieve metadata for the specified object.
 *
 * @since 2.9.0
 *
 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
 * @param int    $object_id ID of the object metadata is for
 * @param string $meta_key  Optional. Metadata key. If not specified, retrieve all metadata for
 * 		                    the specified object.
 * @param bool   $single    Optional, default is false.
 *                          If true, return only the first value of the specified meta_key.
 *                          This parameter has no effect if meta_key is not specified.
 * @return mixed Single metadata value, or array of values
 */
function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) {
	if ( ! $meta_type || ! is_numeric( $object_id ) ) {
		return false;
	}

	$object_id = absint( $object_id );
	if ( ! $object_id ) {
		return false;
	}

	/**
	 * Filters whether to retrieve metadata of a specific type.
	 *
	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
	 * object type (comment, post, or user). Returning a non-null value
	 * will effectively short-circuit the function.
	 *
	 * @since 3.1.0
	 *
	 * @param null|array|string $value     The value get_metadata() should return - a single metadata value,
	 *                                     or an array of values.
	 * @param int               $object_id Object ID.
	 * @param string            $meta_key  Meta key.
	 * @param bool              $single    Whether to return only the first value of the specified $meta_key.
	 */
	$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );
	if ( null !== $check ) {
		if ( $single && is_array( $check ) )
			return $check[0];
		else
			return $check;
	}

	$meta_cache = wp_cache_get($object_id, $meta_type . '_meta');

	if ( !$meta_cache ) {
		$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
		$meta_cache = $meta_cache[$object_id];
	}

	if ( ! $meta_key ) {
		return $meta_cache;
	}

	if ( isset($meta_cache[$meta_key]) ) {
		if ( $single )
			return maybe_unserialize( $meta_cache[$meta_key][0] );
		else
			return array_map('maybe_unserialize', $meta_cache[$meta_key]);
	}

	if ($single)
		return '';
	else
		return array();
}

参考リンク