WordPress tip: Insert posts programmatically

Just paste the following code anywhere on WordPress theme files. If you want to test, I recommend pasting it in your functions.php file.
That’s all you have to do. Once executed, this code will insert a new post into WordPress database.

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);

Thanks to Matt Harzewski for this great piece of code!
Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!WordPress tip: Insert posts programmatically

You Might Also Like...

  • WordPress hack: Insert comments programatically

    2732 readersSome time ago, I've shown you how to insert posts programatically in WordPress database. So now, what about comments? In this recipe I'll show you Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!WordPress hack: Insert comments programatically

  • Add categories or tags to post programatically

    936 readersI recently shown you jow you can create a post or a comment programatically, which is very usefull when creating advanced WOrdPress themes or plugins. Today, let's have a look at another killer snippet: Add categories or tags to a posts, programatically.Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you

  • WordPress hack: Get popular posts by comments count

    2642 readersIn blogging, it is useful to be able to display your most popular posts. There's WordPress plugins to do that, but you don't need it: This code will display your most popular posts, accordoing to the comments count, without requiring you to use a plugin.Looking for WordPress hosting? Try WP Web Host. Prices starts at

  • WordPress hack : How to display ads on old posts only

    312 readersIt is a well known fact that your regular visitors don't click on your ads. In order to monetize your blog without annoying your loyal readers, what about displaying ads only when posts are older than 15 days?Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for

  • WordPress tip: Insert custom content after each post

    1668 readersFor some reason, you may want to be able to automatically display a custom text below each of your posts. You can hard-code it, but it will be way better to do so using a WordPress hook. Here's how to do.Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can

  • How to remove “private” and “protected” from the post title

    2005 readersEach time you define a specific post as being private or password-protected, WordPress automatically add "Private" or "Protected" to your blog post title. If you don't want it, nothing simpler: Just apply this great hack.Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!How to remove

  • WordPress tip: allow upload of more file types

    1615 readersFor some reason, WordPress uploader won't let you upload some filetypes, such as Textmate's .tmCommand. If you need to upload those files to your WordPress blog, here is a very nice snippet that will allow you to do it. Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try

  • How to display custom post types on your WordPress blog homepage

    1847 readersWordPress 3.0 will allow you to create custom post types, so what about being able to list those custom types on your blog homepage? This very useful piece of code will show you how you can do it.Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for

  • WordPress hack: Remove admin name in comments class

    2616 readersBy default, when the blog admin left a comment on his blog, WordPress use the name in the comment css class. This is useful for styling, but it will also let people know about your admin login name. If you want to hide this info, read this recipe.Looking for WordPress hosting? Try WP Web Host.

  • Display dates as “time ago”, the easy way

    1612 readersWant to display human readable dates on your blog, such as "Posted 6 days ago?" There's a lot of available functions to do so, but only a few people know that WordPress have its own built-in function to do that. Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can