使用wp_insert_post()创建一个新页面

时间:2012-12-12 20:29:53

标签: wordpress-plugin wordpress

我在PHP函数中有以下代码,当我安装允许您创建帖子或页面的插件时,该函数被激活。

如果$post_type是“发布”,则完美并制作页面,但如果$post_type是“页面”,则它不起作用,不会创建页面。:

$my_post = array(
  'post_title'    => 'My page Reql',
  'post_type'     => 'page',
  'post_name'     => 'my-page',
  'post_content'  => 'This is my page reql.',
  'post_status'   => 'publish',
  'comment_status' => 'closed',
  'ping_status' => 'closed',
  'post_author' => 1,
  'menu_order' => 0
);

wp_insert_post( $my_post );

有什么问题?我找不到解决方案。

非常感谢!

1 个答案:

答案 0 :(得分:7)

我认为你必须设置guid,就像这样

$PageGuid = site_url() . "/my-page-req1";
$my_post  = array( 'post_title'     => 'My page Reql',
                   'post_type'      => 'page',
                   'post_name'      => 'my-page',
                   'post_content'   => 'This is my page reql.',
                   'post_status'    => 'publish',
                   'comment_status' => 'closed',
                   'ping_status'    => 'closed',
                   'post_author'    => 1,
                   'menu_order'     => 0,
                   'guid'           => $PageGuid );

$PageID = wp_insert_post( $my_post, FALSE ); // Get Post ID - FALSE to return 0 instead of wp_error.