Custom function saves post twice (creates two revisions)

I have a custom function on my theme that gets all posts that are publish, assigns a number, saves that number to a custom and then changes the permalink to the number that's been saved in a custom field.

My problem is that when I save a post, it saves it twice and creates two revisions and all posts saved before get revised as well. (For example: if I have five posts, the first post will have 10 revisions).

Here's my code so far:

function updateNumbers( $post_id ){
    global $pagenow;
    if ($pagenow == 'post.php' &&  'post' == get_post_type()) {
        global $wpdb;
        $querystr = "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE
        $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' ";
        $pageposts = $wpdb->get_results($querystr, OBJECT);
        $counts = 0 ;
        if ($pageposts):
        foreach ($pageposts as $post):
        $counts++;
        add_post_meta($post->ID, 'incr_number', $counts, true);
        remove_action('save_post', 'updateNumbers');
        wp_update_post(array('ID' => $post->ID,'post_name' => get_post_meta($post->ID,'incr_number', true)));
        endforeach;
        endif;
    }}
add_action('save_post', 'updateNumbers');

Thanks.

  • Dimitris
    • Support Star

    Hey there Gregory,

    hope you're doing good and thanks for reaching us! :slight_smile:

    Could you please try to re-add the save_post action right after the wp_update_post() function?
    Like:

    remove_action('save_post', 'updateNumbers');
    wp_update_post(array('ID' => $post->ID,'post_name' => get_post_meta($post->ID,'incr_number', true)));
    add_action('save_post', 'updateNumbers');

    Ref: https://codex.wordpress.org/Plugin_API/Action_Reference/save_post#Avoiding_infinite_loops

    To stop revisions completely, try to use instead:

    remove_action('save_post', 'updateNumbers');
    remove_action('pre_post_update', 'wp_save_post_revision');
    wp_update_post(array('ID' => $post->ID,'post_name' => get_post_meta($post->ID,'incr_number', true)));
    add_action('pre_post_update', 'wp_save_post_revision');
    add_action('save_post', 'updateNumbers');

    Warm regards,
    Dimitris

  • Gregory
    • Site Builder, Child of Zeus

    Thanks for the reply but I get three revisions when I update. Now looking at 'wp-insert-data'. I have a working solution but after the 10th publish post, 10-2 is added to the permalink on all posts and 10 is saved to the custom field

    function updateNumbers( $data, $postarr ) {
        if ( !empty( $postarr['ID'] ) && ! wp_is_post_revision( $postarr['ID'] ) ) {
            if ( !( $counts = get_post_meta( $postarr['ID'], 'incr_number', true ) ) ) {
                global $wpdb;
                $counts = (int)$wpdb->get_var( "SELECT MAX(meta_value) FROM {$wpdb->postmeta} WHERE meta_key='incr_number'" );
                $counts++;
                update_post_meta( $postarr['ID'], 'incr_number', $counts );
            }
            $data['post_name'] = $counts;
        }
        return $data;
        }
    add_filter('wp_insert_post_data', 'updateNumbers', 10, 2 );

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.