我正在构建一个脚本,从外部源创建新的博客帖子。我的代码如下:
// Load WordPress
require_once \'../../wp-load.php\';
require_once ABSPATH . \'/wp-admin/includes/taxonomy.php\';
// Set the timezone so times are calculated correctly
date_default_timezone_set(\'Europe/London\');
// Create post
$id = wp_insert_post(array(
\'post_title\' => $headline,
\'post_content\' => $body,
\'post_date\' => date(\'Y-m-d H:i:s\'),
\'post_author\' => $user_id,
\'post_type\' => \'post\',
\'post_status\' => \'draft\',
));
if($id){
// Set category - create if it doesn\'t exist yet
wp_set_post_terms($id, wp_create_category($region), \'category\');
// Add meta data, if required
add_post_meta($id, \'meta_key\', $metadata);
echo $open_wrap."<h2>Success!</h2>
<p>The post has been added to the Bulletins Category as a <strong>draft</strong>.<br>
Please $wp_url to Publish or Schedule the post.</p>
".$close_wrap;
} else {
echo "WARNING: Failed to insert post into WordPress\\n";
}
我想允许用户设置发布日期,这样他们就可以创建帖子,并在该日期自动发布。
是否有用于添加发布日期的wordpress功能?
SO网友:Johansson
我不知道你为什么要通过包含核心文件来加载WordPress,这一点应该有问题。
无论如何,要安排活动,可以使用wp_schedule_single_event
. 此函数接受3个参数:
wp_schedule_single_event( $timestamp, $hook, $args );
在你的情况下,你可以
wp_insert_post
,然后在时间到达时调用它:
// Use this instead of wp_insert_post
wp_schedule_single_event( \'SET THE TIME HERE\', \'schedule_my_post\' );
// Add an action that runs the function
add_action( \'schedule_my_post\',\'publish_my_post\' );
// Now, do the actual publish
function publish_my_post($args){
wp_insert_post($args);
}
完成。