How to Insert Post Programmatically in WordPress (PHP Code Snippets)

Are you looking for an efficient and automated way to add posts to your WordPress website? While the WordPress dashboard works great for managing your content sometimes you want to programmatically add content to your site. Programmatically inserting posts into your WordPress site will help you save time and effort.

In this post, we’ll show you how to insert posts programmatically in WordPress.

What is Programmatically Inserting Posts?

Programmatically inserting posts is a process of using code to add posts to your WordPress website. This is different from manually adding posts via the WordPress dashboard, which can require more time, effort, and manual input.

With programmatically inserting posts, you can create efficient, automated processes that can save you time and make the post creation process easier.


Why Insert Posts Programmatically in WordPress?

Creating automated processes for adding posts can be a time-saver, especially when it comes to WordPress. With the right code, inserting posts into WordPress can be done quickly and easily.

You’ll want to build around the code snippets we use in this article. For example adding a loop can help you insert an infinite amount of articles into your site.

This is helpful for a few types of sites:

  • Huge sites that need to scale
  • Automated content management
  • Creating a function to insert user’s content

Now let’s dive into inserting a post!


Insert Post Programmatically in WordPress (PHP)

We’ll be using the wp_insert_post() function to add posts to our WordPress site. This function allows you to pass an array with all of your post information to have it instantly added to your site.

The function has plenty of options including post status, author, title, and of course content.

<?php
// Insert post programmatically
$new_post = array(
'post_title' => 'My new post',
'post_content' => 'Content to insert.',
'post_status' => 'publish'
);
$post_id = wp_insert_post( $new_post );
if( $post_id ){
echo "Post inserted successfully with the post ID of ".$post_id;
} else {
echo "Error, post not inserted";
}

Once the code runs, it’ll add a post instantly, so you’ll want to make sure it’s inside a function that doesn’t run all the time. This way, you can keep your posts from repeating and you won’t have to worry about them being posted multiple times.


Insert Custom Post Type Content Programmatically in WordPress (PHP)

The wp_insert_post() function also allows you to add posts to a custom post type. Whether you’re using post types for portfolio items or a niche use case this will work wonders.

<?php
// Insert custom post programmatically
$new_post = array(
'post_title' => 'My new example post',
'post_content' => 'My new content!',
'post_status' => 'public',
'post_type' => 'my_post_type'
);
$post_id = wp_insert_post( $new_post );
if( $post_id ){
// Update custom field on the new post
update_post_meta( $post_id, 'my_custom_field', 'Hello!' );
} else {
echo "Error, post not inserted";
}

wp_insert_post() returns the new post ID which is great for also updating a custom field. In this example I also included updating a custom field after the post is submitted.


Update Post Programmatically in WordPress (PHP)

If you want to update existing posts it’s as easy as adding the ID field to your function. Using an ID of any existing post will update the post with the new information you set. You can leave any field untouched to not modify it. For example if you only wanted to update the post_title you’ll want to remove the post_content line of my example function.

<?php
// Update a post programmatically
$my_post_id = 15
$update_post = array(
'ID' => $my_post_id,
'post_title' => 'My new post title',
'post_content' => 'Overwrite post content',
'post_status' => 'public'
);
wp_insert_post( $update_post );

Conclusion

Programmatically inserting posts in WordPress is an excellent way to save time and effort when adding posts to your website. By using code, you can create efficient, automated processes that make the post creation process much easier.

We hope this post has helped you learn how to insert posts programmatically in WordPress.

If you have any other questions about WordPress development just let us know in the comments below.

Andy Feliciotti

Andy Feliciotti

Andy has been a full time WordPress developer for over 10 years. Through his years of experience has built 100s of sites and learned plenty of tricks along the way. Found this article helpful? Buy Me A Coffee

2 Responses

  1. Excellent article, but how do I include in the import attachments of the post from the external php website and the associated photos?

Leave a Reply

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

WordPress Tips Monthly
Get the latest from SmartWP to your inbox.