Creating a new WordPress post programatically using PHP

In this article, we will discuss the process of creating a new WordPress post programatically. By automating the post creation process, you can save time and effort. Whether you are a developer or a site owner, this technique can be highly beneficial.

Why create a new WordPress post programatically?

Creating a new post programatically allows you to customize the post creation process according to your needs. It gives you full control over the post content, including the title, content, category, tags, and other meta information.

If you want to integrate your WordPress site with an external system or automate content publishing, creating a new post programatically is the way to go.

Step-by-Step Guide to Creating a New WordPress Post Programatically

Follow the steps below to create a new WordPress post programatically:

Create a new post: you can begin creating a new post. Utilize the following PHP code:


/** Loads the WordPress Environment and Template */
require_once __DIR__ . '/wp-load.php';

$author_id = 1; // Replace with the desired author ID

$title = "New WordPress Post Programatically"; // Replace with the desired post title

$content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; // Replace with the desired post content

$post_data = array(

    'post_author' => $author_id,

    'post_title' => $title,

    'post_content' => $content,

    'post_status' => 'publish',

    'post_category' => array(1), // Replace with the desired category ID


$post_id = wp_insert_post($post_data);

if (is_wp_error($post_id)) {

$error_message = $post_id->get_error_message();

echo "Error creating post: " . $error_message;

} else {

echo "Post created successfully! ID: " . $post_id;



Customize the post

After creating the post, you can further customize it by adding meta information, tags, and custom fields. Use the following PHP code as an example:

// Adding tags to the post
wp_set_post_tags($post_id, 'WordPress, new post, programatically');

// Assigning a featured image
$featured_image = ''; // Replace with the URL of the desired image
$image_id = wp_insert_attachment($featured_image, $post_id);

// Adding custom fields
update_post_meta($post_id, 'custom_field_1', 'value_1');
update_post_meta($post_id, 'custom_field_2', 'value_2');

Publish the new post: After customizing the post, you can publish it using the following PHP code:


// Change the post status to 'publish'
$update_post = array(
    'ID' => $post_id,
    'post_status' => 'publish'


Creating a new WordPress post programatically gives you the flexibility to automate the content creation process and integrate your website with external systems. By following the step-by-step guide in this article, you can easily create new posts programmatically using PHP.

Remember, automating the post creation process can save you time and effort, especially if you need to publish a large number of posts or integrate your WordPress site with other platforms. Use the techniques mentioned in this guide to customize your post content, add meta information, and publish the posts seamlessly.

For more information about creating new WordPress posts programmatically, visit the official WordPress Developer documentation.

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

Rolar para cima