How to Programmatically Create a WordPress Post Without Plugins in 3 Easy Steps – 2023

Hey WordPress enthusiasts! We’re back again with another exciting and code-filled tutorial. This time, we’re going to create a WordPress post, not manually, but programmatically! Get ready to take a deep dive into the code!

Step 1: Open Your Functions.php File

The functions.php file is the beating heart of your WordPress theme. It’s where you can add your own custom functions to enhance your theme’s capabilities. So, let’s start by opening this file. You can find it in your current theme’s directory. The path usually looks something like this: /wp-content/themes/your-theme/functions.php.

Step 2: Add a New Function

Once you’ve opened the functions.php file, it’s time to add a new function. This function will create a new WordPress post for us. But how? Well, WordPress provides a handy function called wp_insert_post() which does exactly that. It inserts a new post into the database. Let’s create our function:

				
					function create_my_post() {
    // Post data
    $my_post = array(
        'post_title'    => 'My awesome post',
        'post_content'  => 'This is the content of my awesome post.',
        'post_status'   => 'publish',
        'post_author'   => 1,
        'post_category' => array(8,39)
    );

    // Insert the post into the database
    wp_insert_post( $my_post );
}

				
			

In the above code, we’re creating a new post with the title ‘My awesome post’, some content, a status of ‘publish’, an author ID of 1, and assigning it to categories with the IDs 8 and 39.

Step 3: Call Your Function

Now, we have our function ready, but it’s not doing anything yet because we haven’t called it. To call this function, you can add the following code after the function:

				
					create_my_post();

				
			

But beware! With the function call here, a new post will be created every time WordPress loads. We don’t want that, do we? Instead, you could call this function in response to a specific event, like form submission, button click, or something else.

Here’s the Complete Code:

				
					function create_my_post() {
    // Post data
    $my_post = array(
        'post_title'    => 'My awesome post',
        'post_content'  => 'This is the content of my awesome post.',
        'post_status'   => 'publish',
        'post_author'   => 1,
        'post_category' => array(8,39)
    );

    // Insert the post into the database
    wp_insert_post( $my_post );
}

// Uncomment the following line to create a new post on page load.
// create_my_post();

				
			

As you can see, creating posts programmatically in WordPress is as simple as that. This gives you the power to generate content dynamically and could be especially handy in certain scenarios.

Remember, with great power comes great responsibility. Be careful about when and how you call your function to avoid any unintended consequences like flooding your site with new posts.

Happy coding, everyone!

Table of Contents

Pro Feature

When to Use This Feature

Use Examples