Create a new page with wp_insert_post ()

2019-02-20 15:39发布

I have the following code in a PHP function that is activated when I install my plugin that lets you create a post or page.

Works perfect and make the page if the $post_type is "post", but if the $post_type is "page", then it does not work, does not create the page.:

$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 );

What is the problem? I can not find the solution.

Thank you very much!

1条回答
地球回转人心会变
2楼-- · 2019-02-20 16:09

I think you have to set the guid too, like this

$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.
查看更多
登录 后发表回答