Support

Account

Home Forums Add-ons Repeater Field Remove ACF programatically

Solved

Remove ACF programatically

  • Let’s say I have a Custom Post Type called “Film”, which is related to a Field Group called “Film info”, which contains a Repeater Field called “showings”, which has subfields “code” and “datetime”.

    I want to programatically remove some of the showings (i.e., some of the rows in the repeater field). Concretely I want to remove those where the subfield “datetime” has a value of a date in the past…

    I don’t want to delete the fields, but delete the values added by the user… I mean, I want exactly the same effect that I’d have if I go to all the posts and manually click the “-” to delete rows of the repeater…

    Is there any easy way to do this?

    ———-

    EDIT: Ok, I think I figured out how to do it, using update_field() method like this:

    foreach ( $all_films as $film ) {            
        $showings = get_field( self::SHOWINGS_REPEATER_FIELD_KEY, $film->id );
        $future_showings = array();            
        foreach ( $showings as $showing ) {
            if ( $showing['datetime'] > time() ) {
                $future_showings[] = $showing;
            } 
        }
        update_field( self::SHOWINGS_REPEATER_FIELD_KEY, $future_showings, $film->id);
    }

    And it almost work. Now the problem is that the field ‘datetime’ is of type “Date & Time Picker” using that add-on. And I don’t know why, but after running update_field(), the values of the “datetime” fields have gone…

    It completely removes those showings in the past and only those in the past, which is perfect, but it also deletes the values of the “datetime” fields on those future showings!

    I’m sure it’s related to the Date&Time add-on, because if I change the field to a normal ACF Date picker, it works…

    Any help please??

    —-

    EDIT 2: Ok, I discovered the problem comes when I use the option “get as timestamp” in the Date&Time Picker field… I changed it to get as formatted string and it’s working now…
    Why does this happen, I have no idea!

Viewing 1 post (of 1 total)

The topic ‘Remove ACF programatically’ is closed to new replies.