Creating and inserting new posts in WordPress with wp_insert_post

The wp_insert_post function in WordPress is used to insert a new post into the database. This function can be useful for creating new posts programmatically, such as when importing content from another source or when automating the creation of posts based on certain criteria.

By using the wp_insert_post function, developers can easily add new content to their WordPress site without having to manually create each post through the admin interface. This can save time and streamline the process of adding new content to a website.

Parameters accepted by the wp_insert_post function

  • $postarr (array, required): An array of elements that make up a post to update or insert
  • $wp_error (bool, optional, default value: false): Whether to return a WP_Error on failure
  • $fire_after_hooks (bool, optional, default value: true): Whether to fire the after insert hooks

Value returned by the wp_insert_post function

The function returns an int representing the post ID on success, and the value 0 or a WP_Error on failure.

Examples

How to insert a new post with a title and content

$post = array(
 'post_title' => 'New Post Title',
 'post_content' => 'This is the content of the new post.',
 'post_status' => 'publish'
);

$result = wp_insert_post($post);

This code snippet creates a new post with a title “New Post Title” and content “This is the content of the new post.” The post is set to be published.

How to insert a new post with a specific post type and taxonomy terms

$post = array(
 'post_title' => 'New Post Title',
 'post_content' => 'This is the content of the new post.',
 'post_status' => 'publish',
 'post_type' => 'custom_post_type',
 'tax_input' => array(
 'custom_taxonomy' => array( 'term1', 'term2' )
 )
);

$result = wp_insert_post($post);

This code snippet creates a new post with a title “New Post Title” and content “This is the content of the new post.” The post is of a custom post type “custom_post_type” and is assigned taxonomy terms “term1” and “term2” in the “custom_taxonomy” taxonomy.

How to insert a new post with custom fields/meta data

$post = array(
 'post_title' => 'New Post Title',
 'post_content' => 'This is the content of the new post.',
 'post_status' => 'publish'
);

$post_id = wp_insert_post($post);

update_post_meta($post_id, 'custom_field1', 'value1');
update_post_meta($post_id, 'custom_field2', 'value2');

This code snippet creates a new post with a title “New Post Title” and content “This is the content of the new post.” The post is set to be published, and then it adds custom fields/meta data “custom_field1” with value “value1” and “custom_field2” with value “value2” to the newly created post.

Conclusion

In conclusion, the wp_insert_post function is a powerful component for programmatically adding new posts to a WordPress site. By utilizing this function, developers can easily create and insert posts with custom content, metadata, and post status. Its flexibility and ease of use make it a valuable resource for automating content creation and management within WordPress. With the ability to handle various post types and taxonomies, wp_insert_post is a versatile function that can streamline the process of adding new content to a WordPress site.

Related WordPress Functions