wp_insert_post

Sponsored Link

この関数は、データベースに投稿(およびページ)を挿入します。それは不適切な変数で、日付/時間などのようなミスしている変数内に記入して、幾つかのチェックを行います。それは引数としてオブジェクトを取得し。投稿を作成した投稿IDを返します(あるいは、エラーがある場合0になります)。

Sponsored Link

This function inserts posts (and pages) in the database. It sanitizes variables, does some checks, fills in missing variables like date/time, etc. It takes an object as its argument and returns the post ID of the created post (or 0 if there is an error).

wp_insert_postのパラメーター

$post

(object) (required) 投稿を構成する要素を表すオブジェクトです。データベース内のwp_postsテーブルにそれらの要素とカラムの名前との間に1対1の関係があります。
デフォルト:なし
重要:$post[‘ID’] の値を設定するとID番号を持つ投稿を作成しません。この値を設定すると$postに更新された他の値とID番号である投稿の更新をするための関数を指定します。一言で言えば、新しい投稿を挿入するために、$post[‘ID’]は空白または絶対に設定してはいけません。

(object) (required) An object representing the elements that make up a post. There is a one-to-one relationship between these elements and the names of columns in the wp_posts table in the database.
Default: None
IMPORTANT: Setting a value for $post[‘ID’] WILL NOT create a post with that ID number. Setting this value will cause the function to update the post with that ID number with the other values specified in $post. In short, to insert a new post, $post[‘ID’] must be blank or not set at all.

投稿配列の内容はデフォルトをどの程度(または少し)信頼するかを決めることができます。ここでは、投稿に設定することができる全てのキーの簡単な説明とリストです。:

The contents of the post array can depend on how much (or little) you want to trust the defaults. Here is a list with a short description of all the keys you can set for a post:

$post = array(
  'ID' => [<post id> ] //既存投稿の更新ですか?
  'menu_order' => [<order> ] //新しい投稿がページの場合、順序を設定するにはタブ内に表示するすべきです。
  'comment_status' => [ 'closed' | 'open' ] // 'closed'はコメントがない意味です。
  'ping_status' => [ 'closed' | 'open' ] // 'closed'は、pingbacks または trackbacksが許可されていないの意味です。
  'pinged' => [ ? ] //?
  'post_author' => [<user ID> ] //著者のユーザーID番号です。
  'post_category' => [ array(<category id>,<...>) ] //幾つかのカテゴリーを追加します。
  'post_content' => [<the text of the post> ] //投稿のテキスト全文です。
  'post_date' => [ Y-m-d H:i:s ] //時間投稿が作成された。
  'post_date_gmt' => [ Y-m-d H:i:s ] //GMTで時間投稿が作成された。
  'post_excerpt' => [<an excerpt> ] //すべての投稿抜粋のニーズに対応する。
  'post_name' => [<the name> ] // 投稿(スラッグ)名。
  'post_parent' => [<post ID> ] //新しい投稿の親を設定する。
  'post_password' => [ ? ] //投稿にパスワードですか?
  'post_status' => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' ] //新しい投稿のステータスを設定します。
  'post_title' => [<the title> ] //投稿のタイトルです。
  'post_type' => [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] //定期的に投稿、ページ、リンク、メニューアイテム、あるいは同じカスタム投稿タイプを挿入することができます。
  'tags_input' => [ '<tag>,<tag>,<...>' ] //タグです。
  'to_ping' => [ ? ] //?
  'tax_input' => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // カスタムタクソノミーをサポートします。
);  

$post = array(
‘ID’ => [ ] //Are you updating an existing post?
‘menu_order’ => [ ] //If new post is a page, sets the order should it appear in the tabs.
‘comment_status’ => [ ‘closed’ | ‘open’ ] // ‘closed’ means no comments.
‘ping_status’ => [ ‘closed’ | ‘open’ ] // ‘closed’ means pingbacks or trackbacks turned off
‘pinged’ => [ ? ] //?
‘post_author’ => [ ] //The user ID number of the author.
‘post_category’ => [ array(, < ...>) ] //Add some categories.
‘post_content’ => [ ] //The full text of the post.
‘post_date’ => [ Y-m-d H:i:s ] //The time post was made.
‘post_date_gmt’ => [ Y-m-d H:i:s ] //The time post was made, in GMT.
‘post_excerpt’ => [ ] //For all your post excerpt needs.
‘post_name’ => [ ] // The name (slug) for your post
‘post_parent’ => [ ] //Sets the parent of the new post.
‘post_password’ => [ ? ] //password for post?
‘post_status’ => [ ‘draft’ | ‘publish’ | ‘pending’| ‘future’ | ‘private’ ] //Set the status of the new post.
‘post_title’ => [ ] //The title of your post.
‘post_type’ => [ ‘post’ | ‘page’ | ‘link’ | ‘nav_menu_item’ | custom post type ] //You may want to insert a regular post, page, link, a menu item or some custom post type
‘tags_input’ => [ ‘, , < ...>‘ ] //For tags.
‘to_ping’ => [ ? ] //?
‘tax_input’ => [ array( ‘taxonomy_name’ => array( ‘term’, ‘term2’, ‘term3’ ) ) ] // support for custom taxonomies.
);

注:page_templateは、$postテーブルから削除され、今wp_postmetaテーブル内に配置します。ページのページテンプレートを設定するために、_wp_page_templateのメタキーの代わりにupdate_post_metaを使用します。

注2:post_statusは、投稿ステータスの状態で記述されています。

注3:’予約’のpost_statusを提供している場合、投稿を公開するタイミングを知るためにWordPressにpost_dateを指定する必要があります。

注4:現在のユーザーがカスタムタクソノミーで作業する能力を持っていない場合、タームを追加するためにtax_inputを使用すると動作しません。wp_set_object_terms()を使用する必要があります

NOTE: The page_template key was removed from the $post table and is now located in the wp_postmeta table. To set the page template of a page, use update_post_meta instead with a meta key of _wp_page_template.

NOTE 2: The post_status is documented at Post Status Transitions.

NOTE 3: If providing a post_status of ‘future’ you must specify the post_date in order for WordPress to know when to publish your post.

NOTE 4: If the current user doesn’t have the capability to work with a custom taxonomy then using tax_input to add a term won’t work. You will need to use wp_set_object_terms().

$wp_error

(bool) (optional) 失敗した場合にWP_Errorオブジェクトの戻り値を許可します。
デフォルト:false

(bool) (optional) Allow return of WP_Error object on failure
Default: false

戻り値

投稿のIDは正常にデータベースに追加されている場合です。失敗上、$wp_errorがfalseに設定されている場合0、あるいは$wp_errorがtrueに設定されている場合、WP_Errorオブジェクトを返します。

The ID of the post if the post is successfully added to the database. On failure, it returns 0 if $wp_error is set to false, or a WP_Error object if $wp_error is set to true.

wp_insert_postのファンクションタグ使用方法

<?php wp_insert_post( $post, $wp_error ); ?> 

wp_insert_post()を呼び出す前に、投稿を構成する必要な要素を渡す配列を作成する必要があります。wp_insert_post()はこれらのデフォルトのリストに書き込みますが、ユーザーがタイトルとコンテンツを規定するように義務付けられています。そうしなければデータベースの書き込みが失敗するでしょう。

Before calling wp_insert_post() it is necessary to create an array to pass the necessary elements that make up a post. The wp_insert_post() will fill out a default list of these but the user is required to provide the title and content otherwise the database write will fail.

ユーザーはシンプルにデータベースに新しいキーを定義することによってここにリストされているより多くの要素を提供することができます。キーはデータベース内のwp_postsテーブルに列名と一致している必要があります。

The user can provide more elements than are listed here by simply defining new keys in the database. The keys should match the names of the columns in the wp_posts table in the database.

// 投稿オブジェクトを作成。Create post object
  $my_post = array(
 'post_title' => 'My post',
 'post_content' => 'This is my post.',
 'post_status' => 'publish',
 'post_author' => 1,
 'post_category' => array(8,39)
  );

// データベースに投稿を挿入します。Insert the post into the database
  wp_insert_post( $my_post );

上記に表記されているデフォルトリストは、関数本体で定義されています。それは次のとおりです。

The default list referred to above is defined in the function body. It is as follows:

$defaults = array(
'post_status' => 'draft', 
'post_type' => 'post',
'post_author' => $user_ID,
'ping_status' => get_option('default_ping_status'), 
'post_parent' => 0,
'menu_order' => 0,
'to_ping' =>  '',
'pinged' => '',
'post_password' => '',
'guid' => '',
'post_content_filtered' => '',
'post_excerpt' => '',
'import_id' => 0);

カテゴリー

カテゴリーはデータベース内のカテゴリーIDを一致する整数の配列として渡す必要があります。これは1つのカテゴリーのみが投稿に割り当てられる場合であってもです。

Categories need to be passed as an array of integers that match the category IDs in the database. This is the case even where only one category is assigned to the post.

参照:wp_set_post_terms()

See also: wp_set_post_terms()

セキュリティー

wp_insert_post()は、sanitize_post()を介してデータを渡します。必要な全ての編集と検証(ksesなど)を処理します。

このように、そのことについて心配する必要がありません。

wp_insert_post() passes data through sanitize_post(), which itself handles all necessary sanitization and validation (kses, etc.).

As such, you don’t need to worry about that.

ソースファイル

wp_insert_post()は、wp-includes/post.php内に位置しています。

wp_insert_post() is located in wp-includes/post.php.

関連ファンクションタグ

wp_update_post(), wp_delete_post(), wp_publish_post(), wp_delete_attachment(), wp_get_attachment_url(), wp_insert_attachment(), wp_insert_post_data()

Sponsored Link