• I’m working on a plugin that creates pages based on searches that are run. I want the new pages to be children of a common results page. For some reason, wp_insert_post is not setting post_parent when pages are created. The relevant parts of the code are as follows:

    class wm_mypost {
    var $post_title;
    var $post_content;
    var $post_status;
    var $post_author;
    var $post_name;
    var $post_type;
    var $post_parent;
    var $comment_status;
    var $post_category;
    var $tags_input;
    }

    function createPost($searchTerms, $parentID) {
    $wm_mypost = new wm_mypost();
    $wm_mypost->post_title = $searchTerms;
    $wm_mypost->post_content = “[” . $searchTerms . “]”;
    $wm_mypost->post_status = ‘publish’;
    $wm_mypost->post_author = 1;
    $wm_mypost->post_type = ‘page’;
    $wm_mypost->comment_status = ‘open’;
    $wm_myPost->post_parent = $parentID;
    $wm_mypost->tags_input = $searchTerms;

    $post_ID = wp_insert_post($wm_mypost);
    return $postID;
    }

    I’ve verified that $parentID contains the correct value when this function is called, but the new page always ends up with a parentID of 0. What am I missing? Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Just want to double check the last two lines for you…

    $post_ID = wp_insert_post($wm_mypost);
    return $postID;

    You are missing an underscore in return $postID;

    Thread Starter lehket

    (@lehket)

    Oh yeah . . . thanks! I actually caught that, too, sometime after I had posted the message. The main problem remains, however. post_parent is not set in the database after the call to wp_insert_post, even though it is set in the $wm_mypost object.

    Thread Starter lehket

    (@lehket)

    Quick update: I can get post_parent set by first using wp_insert_post() to create the page and then wp_update_post() as follows:


    $my_post = array();
    $my_post['ID'] = $post_ID;
    $my_post['post_parent'] = $parentID;
    wp_update_post($my_post);

    But this is a workaround for what looks to me like a bug in wp_insert_post(). So I’m leaving this unresolved at the moment to see if anyone can either (a) point out what I’m doing wrong or (b) confirm that wp_insert_post() really isn’t setting post_parent as it should or (c) possibly even indicate that wp_insert_post() was never meant to set the post_parent (although that seems unlikely).

    Thanks again.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Setting post_parent during wp_insert_post’ is closed to new replies.