Support

Account

Home Forums Backend Issues (wp-admin) Revisions dont’t work, get php errors… Reply To: Revisions dont’t work, get php errors…

  • Thanks for the info @jenwachter .

    FWIW, I’ll post here the fix to somewhat unrelated bugs I was experiencing with revisions that arose from trying to set the title of posts dynamically (based on ACF fields) on save.

    If you try to set native WP fields in a save_post hook (or even acf_save_post), you’ll end up with “extra” bogus revisions, often which don’t restore properly, or restore but ignore ACF changed fields.

    My fix was to listen for the wp_insert_post_data event, which I think ACF itself rides on, and make my change there, only referring to ACF fields by their ID since they wouldn’t be accessible via get_field at this point.

    
    // Update dynamic title on save.
    function bio_dynamic_title( $data, $postarr ) {
        // Bail if this isn't the right post type or if ACF wasn't submitted.
        if ( $data['post_type'] !== 'bio' || !array_key_exists( 'acf', $postarr ) ) {
          return $data;
        }
    
        $title = sanitize_text_field( full_name(
          $postarr['acf']['field_587e607692f49'], // first_name
          $postarr['acf']['field_589a17cb1cdd3'], // middle_initial
          $postarr['acf']['field_587e609192f4a'], // last_name
          $postarr['acf']['field_587e6ee94602b'] // suffix
        ));
    
        $data['post_title'] = $title;
        $data['post_name'] = sanitize_title( $title );
    
        return $data;
      }
      add_filter( 'wp_insert_post_data', 'bio_dynamic_title', 10, 2 );