我终于找到了问题的答案。
要使此代码正常工作,请执行以下操作:
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);
我们需要确保wordpress的引导程序已经启动。。。Wordpress引导程序确保所有Wordpress配置都已加载到内存中。这包括所有核心功能等。
回到“以编程方式插入帖子”的原始问题,我们需要在启动wp引导程序后在适当的位置调用wp\\u insert\\u post()。
为此,创建一个新的php文件,如www.yourdomain。com/wpinstalldir/autoposts。php
<?php
/**
* Writes new posts into wordpress programatically
*
* @package WordPress
*/
/** 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);
?>
现在,您将在www.yourdomain上执行此脚本。com/wpinstalldir/autoposts。php将创建您的帖子。简单明了!
只是添加一行require(dirname(__FILE__) . \'/wp-load.php\');
让一切变得不同。