Insert Post Using Custom Code In WordPress

In this article we will see how to insert post in WordPress using custom code.

To insert post in WordPress I am going to use wp_insert_post() function.

Here is the function syntax:

wp_insert_post($post_data, true);

$post_data => Data which you want to include. like post title, post description, post status etc. in array format

true => It returns the $wp_error should be true of false.

If you want to display error if function fails then you need to setup as true otherwise it should setup as false.

Here is the reference code.

$post_data = [
    'post_type' => 'post',
    'post_status' => 'publish',
    'post_title' => 'Test Post',
    'post_content' => 'Test Post Content'
  $post_id = wp_insert_post($post_data, true);
  echo "<pre>";
  echo "</pre>";


I hope you will get the idea about it. If you still have any issues or questions you can reach out to me through the comment box.


Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories