WordPress has a convenient function that can create a new post: wp_insert_post(). Suppose that you wanted to create a plugin to automatically create weekly roundups of your social media activity. You could gather your Delicious bookmarks, Twitter posts, etc. with SimpleXML, mash the data up into a coherent post, then publish the post.
The function syntax is along these lines:
global $user_ID;
$new_post = array(
'post_title' => 'My New Post',
'post_content' => 'Lorem ipsum dolor sit amet...',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);
One thing I find particularly interesting is the post_type argument. You could change it to “page” to create a page instead, or you could combine wp_insert_post() with the (as of yet ill-documented) custom post type API to create new admin panels for your post types.
You can edit a post by passing a post ID in the appropriate argument, select multiple categories in the array, or do pretty much anything you can do from the Write screen. After creating the initial post, you can even use the ID that is returned to add custom fields. The full list of parameters for wp_insert_post() is available in the Codex.

It’s a great post, very useful.. I’m not into coding so my question is: it is possible to have also an image for the preview in the code?
What do you mean by that? Do you mean having the post excerpt contain an image, a thumbnail assigned to the post, etc.?
I’m looking to add a image attached to the post, or even make it as the thumbnail. Is it possible?
I found a great tutorial adding post and comment programmatically here’s the link http://www.sutanaryan.com/blog.....mmatically
hei, we had similar custom need.
thank you. i have suggestion to give some information to your audience about how to validate their post_content before put into database…to help avoid sql injection
by the way, i have done this and make me try to make pages. i try to make pages but have problem when try to create the pages with its respective template page
Is there a way to add keywords to the post with this method?
Sorry I put keywords, I meant tags.
Thanks
Yes, if you take a look at the Codex page, you’ll see there’s a parameter called “tags_input” that you can pass a comma-delimited string to.
Hi there,
Great post,
Could we imagine to create a page where anyone (including not subscribers) can submit posts ? Have you got an idea how to do that ? I really need this function but a PhP beginner
Thanks a lot !
V.
Yes, this could be used for that. You could build an HTML form that submits to a basic PHP script that takes the $_POST data and uses it with the code in this post. A little bit of Google searching should help you learn how to do it.
If that’s too much effort, the popular Gravity Forms (http://www.gravityforms.com/) plugin provides the functionality you are seeking in an easy-to-use package.
If you need to use a plugin to create forms, then you probably shouln’t be doing things programatically. HTML Forms + jQuery can do much more than GravityForms with not much effort IF you’re a developer. And you can create plugins that don’t have a dependency on a paid plugin.
Hi there,
First of all thanks for this post. I managed to script a auto post using this snippet. But when i insert the post content breaks after ‘ (apostrophe) . If I echo the content every thing comes up OK. Checked the encoding seems to be OK. any one got any idea..?
Thanks.
You have to escape the data probably.
You can use WP function esc_attr
http://codex.wordpress.org/Fun.....e/esc_attr
hi there this is all great but is it possible to also create custom fields using this function? And also to assign a template? I want to basically auto create posts by submitting a title and some content, then use database info to complete the post so that we can scale our workload. however the only thing missing is to be able to add a few custom-fields which we need for each post!
thanks
hello and thanks for the useful post!
well I was wondering if you could help me with something here!
I use this code to create pages automatically in wordpress by activating the theme! but I want the content of the page to be some HTML and PHP code that I have written!
is that possible? and if yes, how is it possible?!
It would be very kind of you to help me with that!
Regards
Amin
You can use HTML in the post content, but not PHP. It’s the same as with the post editor: it won’t process PHP, but you can use shortcodes.
There is a plugin for it:
http://wordpress.org/extend/plugins/exec-php/
Like the above we can add posts programatically.How can i add thumbnails programatically.
Check out wp_insert_attachment() at http://codex.wordpress.org/Fun.....attachment
Small example from John K @ http://stackoverflow.com/quest.....-wordpress:
$post_id = wp_insert_post( $my_post_data );
$attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );