この記事は最後に更新してから1年以上経過しています。

カスタムフィールド関連関数のおさらい

説明

カスタムフィールドは、投稿記事の拡張情報としてよく利用されている。投稿ページで登録した情報をアーカイブや投稿ページで表示しているが、投稿ページを表示する際にカスタムフィールドの値を更新して、サイドバーのようなページ内要素に利用したりしている。

関連関数一覧

まずはカスタムフィールドに関連する関数を一通り列挙してみた。こうして見ると、使ったことがない関数もちらほらある。

関数名機能概要ソースファイルsince
post_custom取得キーを指定してカスタムフィールドの値を取得post-template.php1.5.0
get_post_custom取得カスタムフィールドの値をすべて取得post.php1.2.0
get_post_custom_keys取得カスタムフィールドのキーをすべて取得post.php1.2.0
get_post_custom_values取得カスタムフィールドの値を取得post.php1.2.0
get_post_meta取得カスタムフィールドの値を取得post.php1.5.0
get_metadata取得カスタムフィールドの値を取得meta.php2.9.0
add_post_meta追加カスタムフィールドの値を追加post.php1.5.0
add_metadata追加カスタムフィールドの値を追加meta.php2.9.0
update_post_meta更新カスタムフィールドの値を更新post.php1.5.0
update_metadata更新カスタムフィールドの値を更新meta.php2.9.0
update_metadata_by_mid更新カスタムフィールドの値を更新meta.php3.3.0
delete_post_meta削除カスタムフィールドの値を削除post.php1.5.0
delete_post_meta_by_key削除カスタムフィールドの値を削除post.php2.3.0
delete_metadata削除カスタムフィールドの値を削除meta.php2.9.0
delete_metadata_by_mid削除カスタムフィールドの値を削除meta.php3.3.0
the_meta表示カスタムフィールドの値を一覧表示post-template.php1.2.0
is_protected_meta調査カスタムフィールドのキーが予約済みか調べるmeta.php3.1.3

※ソースファイルはwp-includesディレクトリにある。

カスタムフィールドの値を取得する

カスタムフィールドの値を取得する関数は、post_custom および get_post_custom_values > get_post_custom > get_post_meta > get_metadata がある。post_custom は投稿IDを指定できず、必ず現在の投稿情報のカスタムフィールドの値を取得するのに対して、そのほかの関数は投稿IDを指定できる(get_post_custom_values および get_post_custom において投稿IDを省略した場合は現在の投稿情報)。

カスタムフィールドは、1つの投稿情報に対して同じキーで複数個設定することができる。例えば、'food'キーで'牛丼'と'ラーメン'を、'drink'で'コーラ'を設定し、'sweets'は未設定の場合、各関数で取得できる値は次のようになる。

関数名キー取得できた値
post_custom'food'{ '牛丼', 'ラーメン' }
'drink''コーラ'
'sweets'false
get_post_custom_values'food'{ '牛丼', 'ラーメン' }
'drink'{ 'コーラ' }
'sweets'NULL
get_post_meta ( $single = false )'food'{ '牛丼', 'ラーメン' }
'drink'{ 'コーラ' }
'sweets'{ }
get_post_meta ( $single = true )'food''牛丼'
'drink''コーラ'
'sweets'''
get_post_custom-{ 'food'=>{ '牛丼', 'ラーメン' }, 'drink'=>{ 'コーラ' }, ほか }

※get_metadata で取得できる値は get_post_meta と同じ。
※get_post_custom で取得できる値(配列)には、通常公開されていない値を含む。

カスタムフィールドの値を設定・更新する

さて一般的なテーマの場合は、カスタムフィールドの値を取得する関数の利用で済むわけだが、投稿ページをカスタマイズしたり、プラグインでカスタムフィールドの値を操作する場合は、update_post_meta や delete_post_meta といった関数を使用することになる。なお、未設定のカスタムフィールドの値を登録する場合は add_post_meta が用意されているが、update_post_meta で代用できるので、add_post_meta を利用することはないだろう。


最終更新 : 2012年06月22日 17:26


お勧め

get_next_comments_link(2024年12月18日 更新)

string get_next_comments_link( [ string $label = '' [ , int $max_page = 0 [ , int $page = null ] ] ] )
次のコメントリンクを取得する。

add_dashboard_page(2022年6月27日 更新)

mixed add_dashboard_page( string $page_title, string $menu_title, mixed string $capability, string $menu_slug [ , mixed $function = '' ] )
ダッシュボードメニューにサブメニューを登録する。

use_block_editor_for_post(2023年4月24日 更新)

bool use_block_editor_for_post( int | WP_Post $post )
投稿がブロックエディターに対応しているか調べる。

wp_image_editor_supports(2012年12月20日 更新)

bool wp_image_editor_supports( [ mixed $args = array() ] )
イメージエディタがサポートしているか調べる。

wp_check_filetype_and_ext(2019年2月23日 更新)

array wp_check_filetype_and_ext( string $file, string $filename [ , array $mimes = null ] )
ファイルタイプと拡張子を調べる。