How to Insert Articles Programmatically in WordPress (PHP code snippet)
1 min read

How to Insert Articles Programmatically in WordPress (PHP code snippet)

Are you looking for an efficient and automated way to add articles to the WordPress website? Although the WordPress dashboard can manage your content well, sometimes you want to add content to your website through programming. Inserting articles programmatically on WordPress websites can help you save time and effort.

In this article, we will show you how to insert articles programmatically in WordPress.

What is the programming method for inserting articles?

Inserting articles through programming is a process of using code to add posts to WordPress websites. This is different from manually adding articles through the WordPress dashboard, which may require more time, effort, and manual input.

By programmatically inserting articles, you can create efficient automated processes, saving time and simplifying the article creation process.

 

Why insert articles programmatically in WordPress?

Creating a program that automatically adds articles can save time, especially in WordPress. With the correct code, inserting a post in WordPress can be done quickly and easily.

You need to build around the code snippets we use in this article. For example, adding a loop can help you insert an unlimited number of articles into a website.

This is helpful for the following types of websites:

  • Large websites that need to be expanded
  • Automatic Content Management
  • Create a function to insert user content

 

Now, let’s start inserting articles!

 

Insert articles programmatically in WordPress (PHP)

We will use the wp_insert_post()function to add articles to the WordPress website. This function allows you to pass an array containing all article information to immediately add it to your website.

This function has many 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 will immediately add an article, so you need to ensure that it is within a function that will not run continuously. This way, you can avoid duplicate publication of articles and don’t have to worry about them being published multiple times.

Programmatically insert custom article type content in WordPress (PHP)

You can also add articles to custom article types through the wp_insert_post() function. Whether you use the article type for your portfolio or for niche use cases, this feature 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 a new article ID, which is very suitable for updating custom fields. In this example, I also include updating custom fields after submitting the article.

Programmatically updating articles in WordPress (PHP)

If you want to update an existing article, simply add an ID field in the function. You can update articles with the new information you set using any existing article ID. You can leave any fields untouched to avoid modification. For example, if you only want to update the post_title, you need to delete the postcontent line in 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 );

Summary

Inserting articles programmatically in WordPress is a time-saving and labor-saving way to add articles on a website. By using code, you can create efficient and automated processes, making the article creation process simpler.

I hope this article can help you understand how to insert articles programmatically in WordPress.

If you have any further questions about WordPress development, please let us know in the comments below.