How to Create a Post in WordPress: Step-by-Step Guide
To create a post in WordPress, go to the admin dashboard, click
Posts > Add New, then enter your content using the block editor and click Publish. You can also create posts programmatically using the wp_insert_post() function in PHP.Syntax
There are two main ways to create a post in WordPress: via the admin dashboard or programmatically using PHP.
- Admin Dashboard: Navigate to
Posts > Add New, enter your post title and content, then clickPublish. - Programmatically: Use the
wp_insert_post()function with an array of post data.
php
<?php $post_data = array( 'post_title' => 'Your Post Title', 'post_content' => 'Your post content goes here.', 'post_status' => 'publish', 'post_author' => 1, 'post_category' => array(8,39) // category IDs ); $post_id = wp_insert_post($post_data); ?>
Example
This example shows how to create a new post programmatically in WordPress using PHP. It sets the title, content, author, status, and categories, then inserts the post into the database.
php
<?php // Prepare post data $new_post = array( 'post_title' => 'My First Programmatic Post', 'post_content' => 'This post was created using wp_insert_post function.', 'post_status' => 'publish', 'post_author' => 1, 'post_category' => array(1) // Default category ); // Insert the post into the database $post_id = wp_insert_post($new_post); if ($post_id) { echo 'Post created successfully with ID: ' . $post_id; } else { echo 'Failed to create post.'; } ?>
Output
Post created successfully with ID: 123
Common Pitfalls
Common mistakes when creating posts in WordPress include:
- Not setting
post_statustopublish, which keeps the post as a draft. - Using invalid or missing
post_authorID, causing errors. - Forgetting to assign categories or tags if needed.
- Trying to create posts without proper permissions or outside WordPress context.
php
<?php // Wrong: Missing post_status, post remains draft $post_data_wrong = array( 'post_title' => 'Draft Post', 'post_content' => 'This post will not be published automatically.' ); // Right: Set post_status to publish $post_data_right = array( 'post_title' => 'Published Post', 'post_content' => 'This post will be published.', 'post_status' => 'publish', 'post_author' => 1 ); wp_insert_post($post_data_right); ?>
Quick Reference
Here is a quick summary of key parameters for wp_insert_post():
| Parameter | Description | Example |
|---|---|---|
| post_title | The title of the post | 'My New Post' |
| post_content | The main content of the post | 'This is the post body.' |
| post_status | Status like 'publish', 'draft', 'pending' | 'publish' |
| post_author | User ID of the author | 1 |
| post_category | Array of category IDs | [1, 2] |
| post_excerpt | Short summary or excerpt | 'Summary text' |
Key Takeaways
Use the WordPress admin dashboard for quick manual post creation.
Use wp_insert_post() in PHP to create posts programmatically with detailed control.
Always set post_status to 'publish' to make the post visible immediately.
Ensure post_author is a valid user ID to avoid errors.
Assign categories and tags as needed for better content organization.