Support

Account

Home Forums Backend Issues (wp-admin) Issue when ACF restores a post's relationship custom field value from a revision

Solving

Issue when ACF restores a post's relationship custom field value from a revision

  • Hi @elliot
    I found out that ACF has an issue when it restores a post’s relationship custom field from a revision.
    The way I see ACF works when WP restores a revision, it gets all the revision post’s custom fields values using a SQL query and updates the parent post’s custom fields’ values using “update_post_meta”. The problem is that relationships are stored as serialized data in DB and if you get a serialized value from DB and then you try to save it as a custom field value using “update_post_meta” without unserialize it first, it will be serialized again before WP saves it in DB. This happens because “update_post_meta” uses “maybe_serialize” function to serialize data if need it before saving it in the DB and if the value passed to this function is a serialized string then it will be serialized again (which is kind of weird) (http://codex.wordpress.org/Function_Reference/maybe_serialize).

    E.g.:
    ACF value from DB: a:2:{i:0;s:2:”36″;i:1;s:2:”43″;}
    ACF value after “update_post_meta”: s:32:”a:2:{i:0;s:2:”36″;i:1;s:2:”43″;}”;

    sometimes I ended up having something like this: s:37:”s:32:”a:2:{i:0;s:2:”36″;i:1;s:2:”43″;}”;”; in my DB – and at this point ACF relationship doesn’t know how to render this in admin edit-post page and it will show that you actually don’t have any relationship options selected.

    I fixed this temporarily by changing the revisions.php file from ACF plugin:

    
    // save data
    if( $rows )
    {
    	foreach( $rows as $row )
    	{
    		if( in_array($row['meta_key'], $fields) )
    		{
    
    			// check if custom field's value was serialized and unserialize it first so WP can save it properly
    			// WP will serialize it if need it before saving into the DB
    			if( is_serialized( $row['meta_value'] ) )
    			{
    				$row['meta_value'] = unserialize( $row['meta_value'] );
    			}
    			update_post_meta( $post_id, $row['meta_key'], $row['meta_value'] );
    		}
    	}
    }
    

    Elliot, if you can add this fix in ACF, it will be great.
    Thanks!

  • This reply has been marked as private.
  • Also I guess there is another ACF issue related to revisions. When you go to a post’s revisions page and try to revert current post to an old revision, everything will work fine, all the post custom fields will be updated just fine (except that relationship issue). At this point WP will create a new revision – which is normal – but this new revision won’t have any custom fields values, just the default WP_Post properties like title, content and stuff like that.

    Could you please look into this issue too?

    Thank you!

  • https://github.com/adamsilverstein/wp-post-meta-revisions

    This “Feature plugin” will eventually be merged into WP Core and addressed some of the issues you highlight.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.