Tuesday, November 11, 2025

3 min read

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

Sometimes you don’t want to create posts manually – you want code to handle it for you. Maybe you’re importing data, generating content from a form, or syncing from another system. WordPress makes this pretty straightforward with wp_insert_post(), and you don’t need any plugins to use it.

Step 1: Open Your functions.php File

First, open your active theme’s `functions.php` file. You’ll find it under `wp-content/themes/your-theme-name/functions.php`. This is where we’ll add the function that creates a post programmatically. If you prefer to keep logic out of the theme, you can also put this code into a small custom plugin instead.

Step 2: Add a Function That Creates a Post

Now we’ll write a function that uses `wp_insert_post()` to create a post. Here’s a simple example you can drop into `functions.php`:

php
1function create_my_post() {
2      // Post data
3      $my_post = array(
4          'post_title'   => 'My awesome post',
5          'post_content' => 'This is the content of my awesome post.',
6          'post_status'  => 'publish',
7          'post_author'  => 1,
8          'post_category'=> array( 8, 39 ),
9      );
10  
11      // Insert the post into the database
12      $post_id = wp_insert_post( $my_post );
13  
14      if ( is_wp_error( $post_id ) ) {
15          // Handle error if needed
16          error_log( 'Post creation failed: ' . $post_id->get_error_message() );
17      }
18  }

In this example we set a title, some content, the status (publish), the author ID, and assign the post to categories with IDs `8` and `39`. `wp_insert_post()` returns the new post ID on success or a `WP_Error` on failure, so we capture that in `$post_id` and do a basic error check.

Step 3: Call the Function Safely

If you just call `create_my_post();` at the bottom of `functions.php`, WordPress will run it on every page load – which means you’ll get a new post every time the site is loaded. That’s usually not what you want.

A quick way to trigger it once while testing is to temporarily hook it into an admin action, then remove it after the post is created. For example:

php
// Temporary: create a post when an admin page loads
  // add_action( 'admin_init', 'create_my_post' );

Uncomment that line, load any admin page once, check that the post was created, then comment it out again. In a real project, you’d usually call `create_my_post()` in response to something specific – for example, when a custom form is submitted, or when you run an import script.

Full Example

php
1function create_my_post() {
2      // Post data
3      $my_post = array(
4          'post_title'   => 'My awesome post',
5          'post_content' => 'This is the content of my awesome post.',
6          'post_status'  => 'publish',
7          'post_author'  => 1,
8          'post_category'=> array( 8, 39 ),
9      );
10  
11      // Insert the post into the database
12      $post_id = wp_insert_post( $my_post );
13  
14      if ( is_wp_error( $post_id ) ) {
15          error_log( 'Post creation failed: ' . $post_id->get_error_message() );
16      }
17  }
18  
19  // For one-time testing, uncomment the line below, reload an admin page, then comment it again.
20  // add_action( 'admin_init', 'create_my_post' );

WordPress Developer Reference – wp_insert_post():

Programmatically creating posts gives you a lot of flexibility for imports, automations, and custom tools. Just be careful about when the code runs so you don’t accidentally flood your site with duplicate posts.

About the Author
Jonas Lindemann

I’m an experienced SEO professional with over a decade of helping over 100 businesses rank higher online, especially local businesses, e-commerce stores and SaaS. As the co-founder of LPagery, I specialize in practical, proven strategies for regular SEO and Local SEO success.