How to create a WordPress post programmatically

In this article, we will see how to create a post in WordPress programmatically. We will use the wp_insert_post() function to do it.

WordPress is a powerful CMS, but there are times when you need to create content programmatically. Maybe you have hundreds of posts that need to be imported from another system, or maybe you have a third party API that provides new posts on a regular basis. Whatever the reason, sometimes it’s just easier and a lot faster to add posts automatically instead of by hand.

The process of creating a post or page programmatically in WordPress is relatively straightforward however, the final solution will depend on your specific case and needs.

wordpress create post programmatically

Reasons you may want to create posts programmatically in WordPress.

There are many reasons you may want to create posts programmatically in WordPress. You can use this functionality to:

  • Create a larger list of posts that you can later publish all at once.
  • Create a single post based on data collected from a form. This feature is useful when creating pages for events or courses with required fields for each course or event.
  • Create a single post based on information from a database, such as an API or user inputted data through the WordPress dashboard. This might be applicable for certain situations like e-commerce stores displaying product descriptions and prices with each product listing page by querying an external system like Amazon’s API service so that it displays those details directly into your website.

Things to have in mind when creating a post programmatically

It’s easy to create a post in WordPress, but there are some pitfalls to be aware of. When creating posts programmatically, it’s important to keep the following rules in mind:

  • The post must go into the right category.
  • The slug of the post (the part of the URL after your domain name) must be unique. This is really important! If you try to submit a post with an existing slug, your script will fail and WordPress will return an error message telling you why it failed and that the slugs already exist on your blog.
  • The author of the new post should have access privileges as defined by user capabilities (e.g., writer). You can set this with “admin_user”, which returns or sets information about users with administrative permissions for a specified blog or site; see below for more details on capabilities checks

Preparing the setup

First, 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.

If you are using a WordPress plugin then you should put your code in the section of your custom snippets plugin and if you are using a WordPress theme then place your code in the the functions.php file of your child theme.

The code that programmatically create WordPress post

The wp_insert_post function is used to create a new post, set its details and save it as a draft or publish it right away. The code to create a WordPress post programmatically will contain the data of various post elements like the post title, content, or meta information as well as the status of the post ( published, draft or pending).

Use the following PHP code to create a new post, set its details and save it as a draft or publish it right away:

This is also where you might be able to specify a more custom post parameters like : WordPress custom post type, advanced custom field data, meta box, post id, post meta and other meta data, custom taxonomy, featured image etc.

You can customize the hook to trigger a post creation whenever you wish. This can be after a plugin or theme is activated, a certain button is pressed etc. One thing that we need to keep in mind is that we need to check if a post with the same slug does not already exist. Luckily WordPress makes is really easy like this:

Testing your code changes

Testing your code is important to make sure it works as intended.

Before you publish your code to the real world, it’s important to test your code in a development environment. This will help you catch any bugs or typos before they become an issue for your website’s users. You can do it by creating a test environment. A test environment is a copy of your production site that includes all the necessary files and settings for testing your code.

To set up a WordPress test environment you will need to install WordPress in a subdirectory on your local computer (e.g., http://localhost/testblog). This will allow you to have access to core files from inside the /wp-content directory without having to upload them via FTP or SSH every time you want to make changes.

Conclusion

In this post, we have looked at how to create WordPress posts programmatically and shared the code snippet that you can use in your plugin or theme files to add new posts automatically. It’s more efficient than creating posts in the WordPress dashboard and gives you more flexibility to create content that fits your needs.