Support

Account

Home Forums Add-ons Flexible Content Field Flexible Content does not delete post meta properly

Solved

Flexible Content does not delete post meta properly

  • I’m using ACF Pro 5.3.1.

    Here is how you can reproduce the scenario:

    I created an acf-field-group containing a single flexible content field. The field has exact two layouts. In each layout there is only one field of type taxonomy:category with option [allow null = yes].

    The chart of the field is like:

    FC <= the field name
    - layout1
    - - label1 [taxonomy=category, allow null = yes]
    - layout2
    - - label2 [taxonomy=category, allow null = yes]
    

    I assigned the field to a post, and filled in label1 and label2 with some existing categories.

    Then I queried the postmeta table in the db, it’s like:

    +---------+---------+--------------+--------------------------------------------+
    | meta_id | post_id | meta_key     | meta_value                                 |
    +---------+---------+--------------+--------------------------------------------+
    |   49378 |    2152 | _edit_last   | 1                                          |
    |   49379 |    2152 | _edit_lock   | 1445422982:1                               |
    |   49388 |    2152 | _layout      | inherit                                    |
    |   49389 |    2152 | fc_0_label1  | a:1:{i:0;s:3:"201";}                       |
    |   49390 |    2152 | _fc_0_label1 | field_5627592ff9da8                        |
    |   49393 |    2152 | fc           | a:2:{i:0;s:7:"layout1";i:1;s:7:"layout2";} |
    |   49394 |    2152 | _fc          | field_5627591cf9da7                        |
    |   49409 |    2152 | fc_1_label2  | a:1:{i:0;s:3:"192";}                       |
    |   49410 |    2152 | _fc_1_label2 | field_56275946f9daa                        |
    +---------+---------+--------------+--------------------------------------------+
    

    In the post editor (wp-admin/post.php?post=2152&action=edit), I clicked the “remove layout” button on Layout1, leaving only Layout2, then updated the post. The postmeta looked like:

    +---------+---------+--------------+--------------------------+
    | meta_id | post_id | meta_key     | meta_value               |
    +---------+---------+--------------+--------------------------+
    |   49378 |    2152 | _edit_last   | 1                        |
    |   49379 |    2152 | _edit_lock   | 1445423001:1             |
    |   49388 |    2152 | _layout      | inherit                  |
    |   49389 |    2152 | fc_0_label1  | a:1:{i:0;s:3:"201";}     |
    |   49390 |    2152 | _fc_0_label1 | field_5627592ff9da8      |
    |   49393 |    2152 | fc           | a:1:{i:0;s:7:"layout2";} |
    |   49394 |    2152 | _fc          | field_5627591cf9da7      |
    |   49417 |    2152 | fc_0_label2  | a:1:{i:0;s:3:"192";}     |
    |   49418 |    2152 | _fc_0_label2 | field_56275946f9daa      |
    +---------+---------+--------------+--------------------------+
    

    My question is suppose ‘fc_0_label1’ should be deleted, but it was not!

    I hope I made myself clear šŸ˜‰

  • No, it wasn’t and this is the way ACF works. Sometimes it leaves some data behind. The reason is that the fields for the layout are not submitted so ACF does not know to delete them. This has been brought up in the past with repeaters as well.

  • You mean the layout deleted in the post editor won’t be submitted?

    Well, I thought ACF could compare the old layouts and new layouts, delete anything that exists in the old layouts but not in the new layout.

    Anyway, thanks for your answer, John.

  • Yes. ACF would need to either keep track of what was deleted and submit some kind of delete flag or it would need to query the database before saving and compare the old data to the new data before saving. The final choice would be to delete all of the old content and then insert the new content.

    I don’t know how practical the first choice in and the last two would significantly slow down the admin because ACF would have to do twice as much work. I’m also not really sure how this would effect revisions.

    If really want that data to be removed then the best solution there is would be to create an acf/save_post filter that runs before ACF does the save. Then you could figure out how to compare the data and do the deleting.

    I’ve never really had a problem with some old data lingering in the db.

  • To use acf/save_post filter is a good suggestion. Thanks.

  • I was having this problem as well and reported it to the ACF support team. It looks like it has been fixed in ACF Pro 5.5.8.

  • The problem seems to be sill thereā€¦ what can we do to hold the database clean?
    Is there ready coded acf/save_post that we can use?
    Our Problem is that the not deleted post-meta data can be found in sql searchesā€¦

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

The topic ‘Flexible Content does not delete post meta properly’ is closed to new replies.