Set post_parent from URL params in post-new.php

… the problem I have now is how to actually set the post_parent on the
$post object … I dont see any WordPress functions to do so…

From the Codex:

$post (array) (required)
An array representing the elements that make
up a post. There is a one-to-one relationship between these elements
and the names of columns in the wp_posts table in the database.

To set your post parent, pass 'post_parent' => <parent_id> as part of the array that you pass to wp_update_post. That part should be right as far as I can tell, but as you say does not seem to work. But read on.

What you are doing creates an infinite Loop when I try it, though, since wp_update_post used wp_insert_post. You hook gets called over and over until the site runs out of memory. I suspect that that is part of the problem here. That can be fixed with this:

function grey_templater_set_post_parent($postid,$post) {
  remove_action('wp_insert_post','grey_templater_set_post_parent',10,2);

What you are also doing, potentially, is setting the post parent on a revision. You need to compensate.

$postid = (wp_is_post_revision( $postid )) ? wp_is_post_revision( $post ) : $postid;

But that still doesn’t work. I am not sure why (I’d be happy if someone could tell me though), but I am skipping over that because this is not the hook I’d use. Even if this worked you’d have two database writes, and you only need one. There is a hook called wp_insert_post_data that runs before the post is inserted/updated at all.

add_action('wp_insert_post_data','grey_templater_set_post_parent',10,2);

function grey_templater_set_post_parent( $data, $postarr) {
  remove_action('wp_insert_post_data','grey_templater_set_post_parent',10,2);
  if ( ! $parentid = grey_get_post_parent_by_url()
          or 'auto-draft' !== $post->post_status )
          return;
    $data['post_parent'] = $parentid; 

    return $data;
}

That does work, assuming your grey_get_post_parent_by_url() function works.