WP-Mix

A fresh mix of code snippets and tutorials

Publish a post via functions.php

Normally users create new posts from the Posts Edit screen in the WordPress Admin, but you can also publish posts from the functions.php file.

function wpmix_publish_post() {
	global $user_ID;
	$new_post = array(
		'post_title' => 'Publish your own 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);
}

The wp_insert_post() function is the magic. Customize the $new_post array and wp_insert_post() will create the post as specified and return the newly generated post’s ID.

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress