how to writes new posts into wordpress programatically

Use the wp_insert_post() function, which takes an array as a parameter.

<?php
/** Make sure that the WordPress bootstrap has run before continuing. */
require(dirname(__FILE__) . '/wp-load.php');

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);
?>
  • post_title: the name of the post.
  • post_content: the content of the post
  • post_status: the post status (published, draft, etc)
  • post_date: use date() or specify a custom date
  • post_author: Author id of the post author
  • post_type: Can be post, page, or a custom post type
  • post_category: An array of categories ids

Published by

@XiaoKyun

双鱼男,过分热心的好人。