How to Create Post In WordPress Programmatically

wordpress programmatically create post

One of the most common question about WordPress post is  – is it possible to create posts programmatically in WordPress? If you want to create a post in WordPress  programmatically, you need to begin by understanding that a WordPress post is a post type just like the custom post types.

This will help you to programmatically create post using the same wp_insert_post() function that I illustrate on the post – How to Create WordPress page programmatically.

In this quick post, I will show you how to create post In WordPress programmatically using a quick code snippet added to your plugin or theme functions.php file.

WordPress Programmatically Create Post

Essentially the code to create WordPress post programmatically will contain the data of the various post elements like the title, content or post meta as well as the status of the post (whether the post is a published post, draft or pending) .

When you have this data in an array, WordPress has a core function that will help with creating and inserting the post into the WordPress database iswp_insert_post()

This array data is passed to the wp_insert_post() function that will  create and insert the post into the WordPress database.

Step By Step Create Post Programmatically WordPress

wordpress programmatically create post

For a start I have the blank WordPress installation without any posts and we will illustrate how to create post In WordPress programmatically. We can now create the WordPress post programmatically  step by step:

  1. The first step, you need to have a place to add this code which can be a plugin or it can be a theme in the functions.php file.
  2. The second step, you need to fill in the post data in the array below to replace the placeholder data I have added in the array. You can give your post the title, content and the status can either be publish or pending. [php] $wordpress_post = array(
    ‘post_title’ => ‘Post title’,
    ‘post_content’ => ‘Post Content’,
    ‘post_status’ => ‘publish’,
    ‘post_author’ => 1,
    ‘post_type’ => ‘page’
    );
    wp_insert_post( $wordpress_post );

    [/php]
  3. In step three, we can go ahead and add this on a hook and call the wp_insert_post() function that will automatically insert the post for us in the database. In this case we can hook on initialization as in the code below. [php] add_action(‘init’, ‘njengah_create_wordpress_post_programmatically’);

    function njengah_create_wordpress_post_programmatically(){

    $wordpress_post = array(
    ‘post_title’ => ‘Post title’,
    ‘post_content’ => ‘Post Content’,
    ‘post_status’ => ‘publish’,
    ‘post_author’ => 1,
    ‘post_type’ => ‘post’
    );
    wp_insert_post( $wordpress_post );

    }

    [/php]
  4. You can change the hook to any other event you wish to create the post when it occurs. You can turn this to a button and the post will be automatically create when the button is clicked.
  5. You can also add more parameter to the array to capture more data.  Finally you should see the post create as you can see on the screenshot below with the data you added to the array. create post programmatically WordPressWhen you are logged in you will see the post appear along with the other WordPress post as shown on the image below :WordPress create post programmatically

Conclusion

In this post, we have looked at how to create WordPress post programmatically and shared the code snippet that you can utilize in your plugin or theme file to create the post automatically. I hope you can now successfully add this code to your plugin and create the WordPress post automatically. If you would like to have more help with this, you can get in touch.

Similar Articles