質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

Q&A

解決済

1回答

1576閲覧

metaboxをデータベースに送信できていない?

destrudo

総合スコア143

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

0グッド

0クリップ

投稿2017/04/27 13:36

編集2017/04/27 14:15

https://www.smashingmagazine.com/2012/11/complete-guide-custom-post-types/
このサイトを元にfunctions.phpに次のコードを追記しました。

php

1//カスタム投稿タイプ 2function my_custom_post_product() { 3 $labels = array( 4 'name' => _x( 'Products', 'post type general name' ), 5 'singular_name' => _x( 'Product', 'post type singular name' ), 6 'add_new' => _x( 'Add New', 'book' ), 7 'add_new_item' => __( 'Add New Product' ), 8 'edit_item' => __( 'Edit Product' ), 9 'new_item' => __( 'New Product' ), 10 'all_items' => __( 'All Products' ), 11 'view_item' => __( 'View Product' ), 12 'search_items' => __( 'Search Products' ), 13 'not_found' => __( 'No products found' ), 14 'not_found_in_trash' => __( 'No products found in the Trash' ), 15 'parent_item_colon' => '', 16 'menu_name' => 'Products' 17 ); 18 $args = array( 19 'labels' => $labels, 20 'description' => 'Holds our products and product specific data', 21 'public' => true, 22 'menu_position' => 5, 23 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ), 24 'has_archive' => true, 25 ); 26 register_post_type( 'product', $args ); 27} 28add_action( 'init', 'my_custom_post_product' ); 29 30//カスタムタクソノミー 31function my_taxonomies_product() { 32 $labels = array( 33 'name' => _x( 'Product Categories', 'taxonomy general name' ), 34 'singular_name' => _x( 'Product Category', 'taxonomy singular name' ), 35 'search_items' => __( 'Search Product Categories' ), 36 'all_items' => __( 'All Product Categories' ), 37 'parent_item' => __( 'Parent Product Category' ), 38 'parent_item_colon' => __( 'Parent Product Category:' ), 39 'edit_item' => __( 'Edit Product Category' ), 40 'update_item' => __( 'Update Product Category' ), 41 'add_new_item' => __( 'Add New Product Category' ), 42 'new_item_name' => __( 'New Product Category' ), 43 'menu_name' => __( 'Product Categories' ), 44 ); 45 $args = array( 46 'labels' => $labels, 47 'hierarchical' => true, 48 ); 49 register_taxonomy( 'product_category', 'product', $args ); 50} 51add_action( 'init', 'my_taxonomies_product', 0 ); 52 53//メタボックス定義 54add_action( 'add_meta_boxes', 'product_price_box' ); 55function product_price_box() { 56 add_meta_box( 57 'product_price_box', 58 __( 'Product Price', 'myplugin_textdomain' ), 59 'product_price_box_content', 60 'product', 61 'side', 62 'high' 63 ); 64} 65 66//メタボックス内容の定義 67function product_price_box_content( $post ) { 68 wp_nonce_field( plugin_basename( __FILE__ ), 'product_price_box_content_nonce' ); 69 echo '<label for="product_price"></label>'; 70 echo '<input type="text" id="product_price" name="product_price" placeholder="enter a price" />'; 71} 72 73//送信されたデータ処理 74add_action( 'save_post', 'product_price_box_save' ); 75function product_price_box_save( $post_id ) { 76 77 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 78 return; 79 80 if ( !wp_verify_nonce( $_POST['product_price_box_content_nonce'], plugin_basename( __FILE__ ) ) ) 81 return; 82 83 if ( 'page' == $_POST['post_type'] ) { 84 if ( !current_user_can( 'edit_page', $post_id ) ) 85 return; 86 } else { 87 if ( !current_user_can( 'edit_post', $post_id ) ) 88 return; 89 } 90 $product_price = $_POST['product_price']; 91 update_post_meta( $post_id, 'product_price', $product_price ); 92}

metaboxが作られたことは確認できたのですが、送信されたデータ処理で混乱しています。
データベースに送信されているということでしょうか。後、ここの部分で

echo '<input type="text" id="product_price" name="product_price" placeholder="enter a price" />';

value=を加えて送信された値をそこに入れたいのですが、どう記述すればいいか分かりませんでした。
value="get_post_meta($post->ID, $key, true)"としましたが、
そのままget_post_meta($post_ID, $key, true)が表示されてしまい、50等、入力された値が格納されていませんでした。$keyの部分もなんと入れればいいか分からなかったです。

以上2点について、混乱していて分かりづらい質問だと思いますが、どなたかアドバイスおねがいします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

カスタムフィールドを作るなら自前実装よりもCustom Field Suite / Smart Custom Fields などプラグインでの実装をお勧めします。


【Custom Field Suite — WordPress Plugins】
https://ja.wordpress.org/plugins/custom-field-suite/

【get - Custom Field Suite】
http://customfieldsuite.com/api/get.html

【【備忘録】【Wordpress】Custom Field Suiteの$cfs->getについて | Studio Coipo】
http://www.coipo.net/lab/402.html


【Smart Custom Fields — WordPress Plugins】
https://wordpress.org/plugins/smart-custom-fields/

【WordPress のカスタムフィールドを簡単・便利に使えるようになるプラグイン「Smart Custom Fields」作った。 – モンキーレンチ】
https://2inc.org/blog/2014/10/09/4426/

【Smart Custom Fieldsでグループの値を出力する | huwahuwa.org】
http://huwahuwa.org/2016/10/18/smart-custom-fieldsでグループの値を出力する/

投稿2017/04/27 17:19

kei344

総合スコア69407

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問