Support » Fixing WordPress » Function wp_insert_post doesn’t work on pages?

  • I am having issues with the usage of wp_insert_post (); to insert WordPress Pages.

    The below code:

    $my_post = array();
    $my_post['post_title'] = 'Search';
    $my_post['post_status'] = 'publish';
    $my_post['post_type'] = 'page';
    $success = wp_insert_post ($my_post);

    Generates the below errors:

    Warning: in_array() []: Wrong datatype for second argument in [...]/wp-includes/post.php on line 1448
    Fatal error: Call to a member function get_page_permastruct() on a non-object in [...]/wp-includes/link-template.php on line 217

    To make your life easier, here is the online source code of post.php and link_template.php.

    Can anybody help me?

Viewing 3 replies - 1 through 3 (of 3 total)
  • I had the same problem.
    It’s because of you use wp_insert_post() before the functionality has been loaded.

    If you write
    $my_post[‘guid’] = ‘something’;
    these error will be resolved, but you’ll get next error.

    What version of WP are you using?

    use wp_insert_post function inside plugin init or after that.

    this error comes when you call the function before wordpress init.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Function wp_insert_post doesn’t work on pages?’ is closed to new replies.