[resolved] Changing post_parent by costum meta box (3 posts)

  1. IM_natascha
    Posted 4 years ago #

    Hey there,

    i am searching for a method, how to edit the post_parent of a post before creating/updating the post.

    I tried the hook 'save_post' which seems to be to late and called too often. I tried 'pre_post_update' too, but still it does not affect the post although i get the parent's ID right.

    Obviously I lack completely knowledge how to change the object $post before it will be inserted into the database.

    Greatful for any help provided,

  2. IM_natascha
    Posted 4 years ago #

    I found this topic named: Setting post_parent during wp_insert_post but this did not help me.

    wp_update_post( array('ID' => $post_id, 'post_parent' => $parent) );

    Seems like i create an infinite loop.

    Anyone who can help?

  3. IM_natascha
    Posted 4 years ago #

    It gave me a headache, but at last i won. ;)

    For everyone who has a similar problem, try this:
    -> hook to 'save_post'
    -> write following code into the callback function
    $wpdb->get_var( $wpdb->prepare("UPDATE $wpdb->posts SET post_parent = %d WHERE ID = %d", $value, $post_id) );

    The infinite loop was created by calling a WP-post-function. This function caused 'save_post'-hooks to be repeatedly called who themself called the WP-post-function again. Finally the request timed out.

    The solution is simple, by inserting directly with the global $wpdb class (this will not call ANY hooks!!).
    Whoever is familiar with MySQL will not need any further support on this, everyone else shouldn't try to destroy their databases and learn MySQL-statements first.

Topic Closed

This topic has been closed to new replies.

About this Topic