Академический Документы
Профессиональный Документы
Культура Документы
CONTENTS
Description
o See also
o Parameters
o Return
o Source
o Changelog
More Information
o Usage
o Notes
o Categories
o Security
Related
o Uses
o Used By
User Contributed Notes
Description #Description
If the $postarr parameter has ‘ID’ set to a value, then post will be updated.
You can set the post date manually, by setting the values for ‘post_date’ and ‘post_date_gmt’
keys. You can close the comments or open the comments by setting the value for
‘comment_status’ key.
sanitize_post()
Top ↑
Parameters #Parameters
$postarr
(array) (Required) An array of elements that make up a post to update or insert.
'ID'
(int) The post ID. If equal to something other than 0, the post with that ID will be
updated. Default 0.
'post_author'
(int) The ID of the user who added the post. Default is the current user ID.
'post_date'
(string) The date of the post. Default is the current time.
'post_date_gmt'
(string) The date of the post in the GMT timezone. Default is the value
of $post_date.
'post_content'
(mixed) The post content. Default empty.
'post_content_filtered'
(string) The filtered post content. Default empty.
'post_title'
(string) The post title. Default empty.
'post_excerpt'
(string) The post excerpt. Default empty.
'post_status'
(string) The post status. Default 'draft'.
'post_type'
(string) The post type. Default 'post'.
'comment_status'
(string) Whether the post can accept comments. Accepts 'open' or 'closed'. Default
is the value of 'default_comment_status' option.
'ping_status'
(string) Whether the post can accept pings. Accepts 'open' or 'closed'. Default is
the value of 'default_ping_status' option.
'post_password'
(string) The password to access the post. Default empty.
'post_name'
(string) The post name. Default is the sanitized post title when creating a new post.
'to_ping'
(string) Space or carriage return-separated list of URLs to ping. Default empty.
'pinged'
(string) Space or carriage return-separated list of URLs that have been pinged.
Default empty.
'post_modified'
(string) The date when the post was last modified. Default is the current time.
'post_modified_gmt'
(string) The date when the post was last modified in the GMT timezone. Default is
the current time.
'post_parent'
(int) Set this for the post it belongs to, if any. Default 0.
'menu_order'
(int) The order the post should be displayed in. Default 0.
'post_mime_type'
(string) The mime type of the post. Default empty.
'guid'
(string) Global Unique ID for referencing the post. Default empty.
'post_category'
(array) Array of category names, slugs, or IDs. Defaults to value of the
'default_category' option.
'tags_input'
(array) Array of tag names, slugs, or IDs. Default empty.
'tax_input'
(array) Array of taxonomy terms keyed by their taxonomy name. Default empty.
'meta_input'
(array) Array of post meta values keyed by their post meta key. Default empty.
$wp_error
(bool) (Optional) Whether to return a WP_Error on failure.
Default value: false
Top ↑
Return #Return
Top ↑
Source #Source
File: wp-includes/post.php
Top ↑
Changelog #Changelog
Version Description
4.4.0 A 'meta_input' array can now be passed to $postarr to add post meta data.
4.2.0 Support was added for encoding emoji in the post title, content, and excerpt.
1.0.0 Introduced.
Changelog
Top ↑
Usage #Usage
Top ↑
Notes #Notes
Categories #Categories
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.
Security #Security
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.
You may wish, however, to remove HTML, JavaScript, and PHP tags from the post_title and any
other fields. Surprisingly, WordPress does not do this automatically. This can be easily done by
using the wp_strip_all_tags() function and is especially useful when building front-end post
submission forms.
Top ↑
Related #Related
Top ↑
Uses #Uses
wp-includes/post.php: wp_add_trashed_suffix_to_post_name_for_trashed_posts()
wp-includes/post.php: wp_add_trashed_suffix_to_post_name_for_post()
wp-includes/post.php: attachment_updated
wp-includes/comment.php: get_default_comment_status()
wp-includes/post.php: add_attachment
Show 58 more uses
Top ↑
Used By #Used By
wp-includes/class-wp-customize-
manager.php: WP_Customize_Manager::save_changeset_post()
wp-includes/theme.php: wp_update_custom_css_post()
wp-includes/rest-api/endpoints/class-wp-rest-attachments-
controller.php:WP_REST_Attachments_Controller::create_item()
wp-includes/rest-api/endpoints/class-wp-rest-posts-
controller.php: WP_REST_Posts_Controller::create_item()
wp-includes/class-wp-customize-nav-
menus.php: WP_Customize_Nav_Menus::insert_auto_draft_post()
Show 10 more used by
Top ↑
You must log in to vote on the helpfulness of this noteVote results for this
note:8You must log in to vote on the helpfulness of this note
Insert post with custom taxonomy and post meta data (since 4.4.0):
1
$hierarchical_tax = array( 13, 10 ); // Array of tax ids.
2 $non_hierarchical_tax = 'tax name 1, tax name 2'; // Can use array of ids or string
3
4 $post_arr = array(
5 'post_title' => 'Test post',
6 'post_content' => 'Test post content',
7 'post_status' => 'publish',
8 'post_author' => get_current_user_id(),
9 'tax_input' => array(
10 'hierarchical_tax' => $hierarchical_tax,
11 'non_hierarchical_tax' => $non_hierarchical_tax,
12 ),
13 'meta_input' => array(
14 'test_meta_key' => 'value of test_meta_key',
),
15 );
16
You must log in to vote on the helpfulness of this noteVote results for this
note:7You must log in to vote on the helpfulness of this note
Inserting a 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.
The next example shows the post title, content, status, author, and post categories being set. You
can add further key-value pairs, making sure the keys match the names of the columns in
the wp_posts table in the database.
You can also get the new post ID after inserting a new post:
You must log in to vote on the helpfulness of this noteVote results for this
note:6You must log in to vote on the helpfulness of this note
By default, wp_insert_post will not insert an empty post. This can cause unexpected problems
if you’re passing an ID and expecting it to fall back to wp_update_post. For instance, the
following will not work:
You must log in to vote on the helpfulness of this noteVote results for this
note:3You must log in to vote on the helpfulness of this note
‘tax_input’ in the arguments only works on wp_insert_post if the function is being called by a
user with “assign_terms” access
https://core.trac.wordpress.org/browser/tags/4.7/src/wp-includes/post.php#L3352
Log in to add feedback
You must log in to vote on the helpfulness of this noteVote results for this
note:2You must log in to vote on the helpfulness of this note
When saving data containing slashes remember to add extra slashes due
to wp_insert_post unslashing these values.
1
$regex = '(\b16-\d{3}\b)';
2 $post_data = array(
3 'post_title' => 'Test regex',
4 'post_content' => $regex,
5 'post_type' => 'post',
);
6 $post_id = wp_insert_post( $post_data );
7
1
$regex = '(\b16-\d{3}\b)';
2 $post_data = array(
3 'post_title' => 'Test regex',
4 'post_content' => wp_slash( $regex ),
5 'post_type' => 'post',
);
6 $post_id = wp_insert_post( $post_data );
7
You must log in to vote on the helpfulness of this noteVote results for this
note:1You must log in to vote on the helpfulness of this note
Its important that you validate your post ID before you use by checking if the returned values is
not a WP_error,
1
2 $args = array(
3 'post_type' => 'my_custom_post',
/*other default parameters you want to set*/
4 );
5 $post_id = wp_insert_post($args);
6 if(!is_wp_error($post_id)){
7 //the post is valid
}else{
8 //there was an error in the post insertion,
9 echo $post_id->get_error_message();
10 }
11
You must log in to vote on the helpfulness of this noteVote results for this
note:1You must log in to vote on the helpfulness of this note
Reference:
https://ryansechrest.com/2012/07/wordpress-taxonomy-terms-dont-insert-when-cron-job-
executes-wp_insert_post/
You must log in to vote on the helpfulness of this noteVote results for this
note:0You must log in to vote on the helpfulness of this note
You must log in to vote on the helpfulness of this noteVote results for this
note:0You must log in to vote on the helpfulness of this note
This snippet creates a post with code checking if the post already exists before creating it.
1 function create_wordpress_post_with_code() {
2
// Set the post ID to -1. This sets to no action at moment
3 $post_id = -1;
4
5 // Set the Author, Slug, title and content of the new post
6 $author_id = 1;
7 $slug = 'wordpress-post-created-with-code';
$title = 'WordPress post created whith code';
8
$content = 'This is the content of the post that we are creating right now
9 More text: I motsetning til hva mange tror, er ikke Lorem Ipsu
10 Dets røtter springer helt tilbake til et stykke klassisk latin
11 hvilket gjør det over 2000 år gammelt. Richard McClintock - pr
12 College i Virginia, USA - slo opp flere av de mer obskure lati
fra en del av Lorem Ipsum, og fant dets utvilsomme opprinnelse
13 av disse ordene i klassisk litteratur. Lorem Ipsum kommer fra
14 "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil)
15 Boken er en avhandling om teorier rundt etikk, og var veldig po
16 linjen av Lorem Ipsum, "Lorem Ipsum dolor sit amet...", er hen
17 // Cheks if doen't exists a post with slug "wordpress-post-created-with-co
if( !post_exists_by_slug( $slug ) ) {
18 // Set the post ID
19 $post_id = wp_insert_post(
20 array(
21 'comment_status' => 'closed',
22 'ping_status' => 'closed',
23 'post_author' => $author_id,
24 'post_name' => $slug,
25 'post_title' => $title,
26 'post_content' => $content,
27 'post_status' => 'publish',
28 'post_type' => 'post'
29 )
30 );
} else {
31
32 // Set pos_id to -2 becouse there is a post with this slug.
33 $post_id = -2;
34
35 } // end if
36
37 } // end oaf_create_post_with_code
38 add_filter( 'after_setup_theme', 'create_wordpress_post_with_code' );
/**
39 * post_exists_by_slug.
40 *
41 * @return mixed boolean false if no post exists; post ID otherwise.
42 */
function post_exists_by_slug( $post_slug ) {
43 $args_posts = array(
44
'post_type' => 'post',
45
'post_status' => 'any',
46
'name' => $post_slug,
47 'posts_per_page' => 1,
48 );
49 $loop_posts = new WP_Query( $args_posts );
50 if ( ! $loop_posts->have_posts() ) {
return false;
51 } else {
52 $loop_posts->the_post();
53 return $loop_posts->post->ID;
54 }
55 }
56
57
58
59
60
61
62
63