[WordPress] Defining custom fields (custom-fields) on multiple custom post pages.

You can find out how to create custom text fields in this article.

From here, you can further create text fields with different roles in the same or other custom posts.
It’s simple to do: group the variables together in an array and define add_meta_box with foreach.

The end result is a text field that looks like this.

目次

Grouping variables into arrays

First, define the required variables in an array with add_meta_box.

In this article, two text fields are set for the custom post type ‘api_member_post’ and one text field for ‘api_works_post’.

Change the id, key, post_type, etc. as required.

const CUSTOM_FIELDS = [
    [
        'id' => 'member_text',// セクションの一意となるHTML ID
        'post_key' => 'memberText', // 投稿を保存する時にPOSTする一意となるなキー
        'title' => 'メンバーの説明文',//編集画面セクションのタイトル
        'post_type' => 'api_member_post' // 投稿タイプ名
    ],
    [
        'id' => 'member_text2',
        'post_key' => 'memberText2',
        'title' => 'メンバーの説明文2',
        'post_type' => 'api_member_post'
    ],
    [
        'id' => 'works_text',
        'post_key' => 'worksText',
        'title' => 'メンバーの説明文',
        'post_type' => 'api_works_post'
    ]
];

Create as many custom text fields as you need.

Turn in CUSTOM_FIELDS to create a text field for each custom post.

The point here is to define an anonymous function in the third argument callback of add_meta_box and pass $field so that the text field can be created dynamically.

use is used within an anonymous function to use variables that are external to the anonymous function.
As an unnamed function can only access variables defined within its scope, the use keyword must be used to access variables outside it.

foreach (CUSTOM_FIELDS as $field) {
    /**
     * カスタムフィールド
     */
    add_action('admin_menu', function() use ($field) {
        add_meta_box(
            $field['id'] . '_setting',
            $field['title'],
            function() use ($field) {
                insert_custom_fields($field);
            }, // 編集画面セクションにHTML出力する関数
            $field['post_type'],
            'normal' // 編集画面セクションが表示される部分
        );
    });
    
    add_action('save_post', function($post_id) use ($field) {
        if (isset($_POST[$field['post_key']])) {
            update_post_meta($post_id, $field['post_key'], $_POST[$field['post_key']]);
        }
    });
}

function insert_custom_fields($field)
{
    $POST_KEY = $field['post_key'];

    global $post;
    $meta = get_post_meta($post->ID, $field['post_key'], true); 
    ?>

        <label for="<?= $POST_KEY ?>"><?= $field['title'] ?></label>
        <input id="<?= $POST_KEY ?>" type="text" name="<?= $POST_KEY ?>" value="<?= $meta ?>">

    <?php
} ?>

Write this in functions.php and edit it in the relevant custom post type so that the custom fields appear at the bottom of the screen.

Summary

When defining multiple definitions of the same thing, it’s only possible to refactor and loop.

よかったらシェアしてね!
目次