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

Nac*_*nto 1 wordpress wordpress-plugin

我在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 );
Run Code Online (Sandbox Code Playgroud)

问题是什么?我找不到解决方案.

非常感谢你!

Fel*_*a A 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.
Run Code Online (Sandbox Code Playgroud)