WordPress.org

Ready to get started?Download WordPress

Forums

Function wp_insert_post doesn't work on pages? (4 posts)

  1. possible248
    Member
    Posted 5 years ago #

    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() [function.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?

  2. a.rodin
    Member
    Posted 4 years ago #

    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.

  3. s_ha_dum
    Member
    Posted 4 years ago #

    What version of WP are you using?

  4. anantshri
    Member
    Posted 4 years ago #

    use wp_insert_post function inside plugin init or after that.

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

Topic Closed

This topic has been closed to new replies.

About this Topic