How To Create Post and Comment in WordPress Programmatically

WordPress comes with lots of handy functions and some of these functions are wp_insert_post() and wp_insert_comment() which has the power to add your data programmatically, in this article; I’ll show you how you can easily add posts and comments to your WordPress site.

To insert post

This snippet is use to insert post programmatically.

Note: The wp_insert_post() return the post ID if if the post is successfully added to the database. On failure, it returns 0 if $wp_error is set to false.



  • post_title: The post title.
  • post_content: The content of the post
  • post_status: The post status (published, draft, etc.)
  • post_author: ID of the post author
  • post_type: Page, Post or Custom post type
  • post_category: Single category or an Array of category ID’s

If you wish to remove HTML, JavaScript, and PHP tags from the post_title and any other fields, you can use other wordpress built-in function wp_strip_all_tags().

Update the above post title into this line of code

To insert comment

This snippet use to inserts a comment to the database.

Note: The wp_insert_comment() return new commend ID if the comment is successfully added to the post/database.


  • comment_post_ID: ID of the commented post
  • comment_author: Name of the comment author
  • comment_author_email: Email address of the comment author
  • comment_author_url: Website of the comment author
  • comment_content: Message of the comment
  • comment_author_IP: IP address of the comment author
  • comment_agent: User agent of the commenter browser
  • comment_date: Date of the comment
  • comment_date_gmt: GMT date of the comment
  • comment_approved: 1 for approve or 0 for “awaiting moderation”

That’s it, if you have suggestion, opinion use the comment below to share your idea.

by Ryan Sutana

  • swati

    wp_insert_comment() is inserting 250 same records in db.

    What cud be the possible error?

    • @swati check your code maybe it is wrap by a loop.

      you can post your code here for us to have a look and find better solution.

  • What I dont recognize is how youre not even much more common than you might be now. Youre just so intelligent. You know so considerably about this subject, made me think about it from so several distinct angles. Its like folks arent interested unless it has some thing to do with Lady Gaga! Your stuffs great. Keep it up!

  • vitorhugobernardescosta

    which file i have to edit? i try on post.php but i’ve got an error. Please help me to solve..

    • Hi @vitorhugobernardescosta,

      You may confuse how to use the code above.

      To use the code above you need to have HTML form first, pass the value using POST or GET then use the passing value, here’s a sample

      // let say we’re using POST method
      $title = $_POST[‘title’];
      $content = $_POST[‘content’];

      // Create post object
      $my_post = array(
      ‘post_title’ => $title,
      ‘post_content’ => $content,
      ‘post_status’ => ‘publish’,
      ‘post_author’ => 1

      // Insert the post into the database
      $post = wp_insert_post( $my_post ); // return post ID on success

      if( $post ) {
      echo ‘Success’;

      To use this code better create new page template file.

      Hope that helps.

      • vitorhugobernardescosta

        thanks for the support