0

I have been trying to update a post in the posts table but for some reason, it is creating a multiple number of revisions but not updating the main post. I have been trying hard for the last one day to figure this out but so far so luck with this.

$post_meta_value = get_post_meta($unit_post_id, $post_meta_key.'_after_id', true);
    global $wpdb;
    $my_post = array(
        'ID'           => $post_meta_value,
        'post_content' => $meta_after_content,
        'post_title'  => $meta_after_title,
        'post_status' => 'publish'
    );
    wp_update_post($my_post);

// Update the post into the database

I have looked at the codex, but I am confused about the action mentioned in the code http://codex.wordpress.org/Function_Reference/wp_update_post

  • In the description on the codex: Note that when the post is "updated", the existing Post record is duplicated for audit/revision purposes. The primary record is then updated with the new values. Category associations, custom fields, post meta, and other related entries continue to be linked to the primary Post record. Does that make sense in your case? – Saurabh Shukla Mar 2 '15 at 11:14
1

check: https://codex.wordpress.org/Function_Reference/wp_update_post

Make sure not to create an infinite loop.

<?php
function my_function( $post_id ){
    if ( ! wp_is_post_revision( $post_id ) ){

        // unhook this function so it doesn't loop infinitely
        remove_action('save_post', 'my_function');

        // update the post, which calls save_post again
        wp_update_post( $my_args );

        // re-hook this function
        add_action('save_post', 'my_function');
    }
}
add_action('save_post', 'my_function');
?>

to stop revisions try to add: https://wordpress.stackexchange.com/a/3398

remove_action('pre_post_update', 'wp_save_post_revision');// stop revisions

and

add_action('pre_post_update', 'wp_save_post_revision');//  enable revisions again

Good luck ;-)

| improve this answer | |
  • How/where would you call remove_action('pre_post_update', 'wp_save_post_revision');? I've got a plugin file with custom function that calls the remove_action -- but I can't figure out what hook to attach my function to. I've tried add_action on save_post & others. Nothing seems to have an effect. (The function is running, but revisions are still created.) The only place I can get the revisions to not be created is with apply_filter on wp_insert_post_data which doesn't create the revision, but also doesn't save the modified post data at all either. @jonathan-joosten – brandonjp Mar 19 at 3:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.