Wordpress Insert Post Programmatically (wp_insert_post) - Complete Guide and Examples

Many times we need to insert a post in wordpress programmatically. For example, we may want to poll certain web api periodically and based on its response insert wordpress post automatically.

To insert a wordpress post, one can use the function wp_insert_post

wp_insert_post( array $postarr, bool $wp_error = false )

wordpress insert post example

Here is a rough example of how to insert a post

$comment_status = 'closed'; $ping_status = 'closed'; $slug = ""; $title = ""; $post_status = "publish"; $post_type = "post"; $post_author_id = "3" ; # id of post author $postid = wp_insert_post( array( 'comment_status' => $comment_status, 'ping_status' => $ping_status, 'post_author' => $post_author_id, 'post_name' => $slug, 'post_title' => $title, 'post_status' => $post_status, 'post_type' => $post_type ) );