Programmatically Creating Posts in WordPress

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.

  • http://www.liquidskydesign.org/ Claudia

    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?

    • http://www.webmaster-source.com Matt

      What do you mean by that? Do you mean having the post excerpt contain an image, a thumbnail assigned to the post, etc.?

      • http://www.pedro-lopes.com Pedro Lopes

        I’m looking to add a image attached to the post, or even make it as the thumbnail. Is it possible?

  • http://www.bakawan.com/log/ uwiuw

    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

  • http://www.trevorbphoto.com Trevor

    Is there a way to add keywords to the post with this method?

  • http://www.trevorbphoto.com Trevor

    Sorry I put keywords, I meant tags.

    Thanks

    • http://www.webmaster-source.com Matt

      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.

  • Vinche

    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.

    • http://www.webmaster-source.com Matt

      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.

      • mario

        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.

  • http://lankagazette.com Chathura

    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.

  • Shal

    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

  • Amin

    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

    • http://www.webmaster-source.com Matt

      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.

      • http://richardmisencik.com/ Richard

        Is it possible to somehow change the post_content conditionally?
        Im using a foreach to add multiple posts at once:

        $add_pages = array(
        ‘post_title’ => $new_page,
        ‘post_content’ => ”, // lets say, if the page_title is ‘Home’, set the post_content to ‘Default home page content’
        ‘post_status’ => ‘publish’,
        ‘post_type’ => ‘page’
        );

    • http://leondoornkamp.nl Léon
  • http://wordpressnotes.blogspot.com Anjali

    Like the above we can add posts programatically.How can i add thumbnails programatically.

  • http://www.sthaanil.com.np Anilou

    There is very useful plugin that will help you to create custom form with any kind of fields you want. The plugin is “custom fields”. You can use “custom post types” together with “custom fields”. You will definitely thanks me if you dont know about this plugin. Just try it…

  • http://www.sthaanil.com.np ANil

    There are lots on plugins that can be used to execute code in post. eg. php exec.

  • http://chittagong.com/ Chittagong

    Do you know any good plugin that does the creating page thing easily without going into code? Non-programmers like me always seek opportunity to grab plugins to sort out things.

  • http://mpsbhat.blogspot.com Pradeep

    Hi…, Is this code creates different pages which have different contents? Please clarify. Because I want to create pages instantly when a surveyor uploads data to database.

  • Joe Bhakti Rendeun

    you need this in header

    define('WP_USE_THEMES', false);
    require('../wp-blog-header.php');