カスタム投稿タイプのフィールドで別の投稿タイプの値を取得・表示 : WordPress

Pocket

2つのカスタム投稿タイプ(shop, staff)を作成する。shopの情報をstaffカスタムフィールドで選択する。

functions.php


<?php
/*
 * Custom Post Type
 * post name shop
 */
add_action( 'init', 'createShop' );
function createShop()
{
    /*
     * custom post type shop
     */
    $labels = array(
        'name'               => 'shops',
        'singular_name'      => 'shop',
        'add_new'            => 'shopを追加',
        'add_new_item'       => '新しいshopを追加',
        'edit_item'          => 'shopを編集',
        'new_item'           => '新しいshop',
        'view_item'          => 'shopを編集',
        'search_items'       => 'shopを探す',
        'not_found'          => 'shopはありません',
        'not_found_in_trash' => 'ゴミ箱にshopはありません',
        'parent_item_colon'  => ''
    );
    $args = array(
        'labels'               => $labels,
        'public'               => true,
        'capability_type'      => 'post',
        'hierarchical'         => false,
        'supports'             => array( 'title' ),
        'has_archive'          => true,
        'rewrite'              => array( 'slug' => 'shop', 'with_front' =>true ),
        'register_meta_box_cb' => 'shop_meta_box'
    );
    register_post_type( 'shop', $args );


    /*
     * custom fields
     */
    function shop_meta_box( $post )
    {
        add_meta_box( 'shop_meta', 'shop情報', 'shop_meta_func', 'shop', 'normal', 'high' );
    }
    function shop_meta_func( $post, $box )
    {
        $shop_name  = get_post_meta( $post->ID, 'shop_name', true );
        echo wp_nonce_field( 'shop_meta', 'my_meta_nonce' );
        // フォーム
        echo '<p>ショップ名 <input type="text" name="shop_name" value="'
            . esc_html( $shop_name ) . '"></p>';
    }
    // save post
    add_action( 'save_post', 'shop_meta_update' );
    function shop_meta_update( $post_id )
    {
        $my_meta_nonce = isset( $_POST[ 'my_meta_nonce' ] ) ? $_POST[ 'my_meta_nonce' ] : null;
        if ( !wp_verify_nonce( $my_meta_nonce, 'shop_meta' ) ) {
            return $post_id;
        }
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return $post_id;
        }
        if ( 'shop' == $_POST[ 'post_type' ] ) {
            if( !current_user_can( 'edit_post', $post_id ) ) {
                return $post_id;
            }
        } else {
            return $post_id;
        }
        $shop_name = $_POST[ 'shop_name' ];
        if( isset( $note ) && '' === $shop_name ) {
            delete_post_meta( $post_id, 'shop_name' );
        } else {
            update_post_meta( $post_id, 'shop_name', $shop_name );
        }
    }
}


/*
 * Custom Post Type
 * post name staff
 */
add_action( 'init', 'createStaff' );
function createStaff()
{
    /*
     * custom post type shop
     */
    $labels = array(
        'name'               => 'staff',
        'singular_name'      => 'staff',
        'add_new'            => 'staffを追加',
        'add_new_item'       => '新しいstaffを追加',
        'edit_item'          => 'staffを編集',
        'new_item'           => '新しいstaff',
        'view_item'          => 'staffを編集',
        'search_items'       => 'staffを探す',
        'not_found'          => 'staffはありません',
        'not_found_in_trash' => 'ゴミ箱にstaffはありません',
        'parent_item_colon'  => ''
    );
    $args = array(
        'labels'               => $labels,
        'public'               => true,
        'capability_type'      => 'post',
        'hierarchical'         => false,
        'supports'             => array( 'title' ),
        'has_archive'          => true,
        'rewrite'              => array( 'slug' => 'staff', 'with_front' =>true ),
        'register_meta_box_cb' => 'staff_meta_box'
    );
    register_post_type( 'staff', $args );


    /*
     * custom fields
     */
    function staff_meta_box( $post )
    {
        add_meta_box( 'staff_meta', 'staff情報', 'staff_meta_func', 'staff', 'normal', 'high' );
    }
    function staff_meta_func( $post, $box )
    {
        // staff name
        $staff_name  = get_post_meta( $post->ID, 'staff_name', true );
        echo wp_nonce_field( 'staff_meta', 'my_meta_nonce' );
        echo '<p>スタッフ名 <input type="text" name="staff_name" value="'
            . esc_html( $staff_name ) . '"></p>';
        // post id of custom post type shop
        $staff_shop  = get_post_meta( $post->ID, 'staff_shop', true );
        $shop_posts = get_posts( array(
            'numberposts'  => 100,
            'post_type'    => 'shop',
        ) );
        echo 'shop: <select name="staff_shop">'. PHP_EOL;
        echo '<option value="-">--</option>';
        foreach( $shop_posts as $shop_post ) {
            // need intval
            if ( $shop_post->ID === intval( $staff_shop ) ) {
                echo '<option value="' . $shop_post->ID . '" selected="selected">' . $shop_post->ID . '</option>';
            } else {
                echo '<option value="' . $shop_post->ID . '">' . $shop_post->ID . '</option>';
            }
        }
        echo '</select>';
        wp_reset_postdata();
    }
    // save post
    add_action( 'save_post', 'staff_meta_update' );
    function staff_meta_update( $post_id )
    {
        $my_meta_nonce = isset( $_POST[ 'my_meta_nonce' ] ) ? $_POST[ 'my_meta_nonce' ] : null;
        if ( !wp_verify_nonce( $my_meta_nonce, 'staff_meta' ) ) {
            return $post_id;
        }
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return $post_id;
        }
        if ( 'staff' == $_POST[ 'post_type' ] ) {
            if( !current_user_can( 'edit_post', $post_id ) ) {
                return $post_id;
            }
        } else {
            return $post_id;
        }
        $staff_name = $_POST[ 'staff_name' ];
        if( isset( $note ) && '' === $staff_name ) {
            delete_post_meta( $post_id, 'staff_name' );
        } else {
            update_post_meta( $post_id, 'staff_name', $staff_name );
        }
        $staff_shop = $_POST[ 'staff_shop' ];
        if( isset( $note ) && '' === $staff_shop ) {
            delete_post_meta( $post_id, 'staff_shop' );
        } else {
            update_post_meta( $post_id, 'staff_shop', $staff_shop );
        }
    }
}

上記方法でカスタム投稿タイプを3つ用いた最小構成のテーマを作成

shop
staff
product
custom_post_type

簡単なイメージ図
https://cacoo.com/diagrams/deSuNCBKDrV53pMM

コメント

No comments yet.

コメントの投稿

改行と段落タグは自動で挿入されます。
メールアドレスは表示されません。