Home Forums ACF PRO Retrieve most recent front-end edited fields? Reply To: Retrieve most recent front-end edited fields?

  • Sometimes I find things interesting and believe it or not, this type of coding is what I do to relax. I’ve done this in OOP because it’s the way I think.

    I’m not sure if there are any errors, I have not tested this. It’s not meant to be exact but it’s easier to give a code example than to explain all the details.

          store updated values for a repeater
          so they can easily be used elsewhere
          This specific class will save new values
          for new images added to a repeater
          there should only be 1 image field in the repeater
      new for_kkranzo_store_recent();
      class for_kkranzo_store_recent {
        // this will store any updates that are made
        private $new_content = array();
        // a WP option name value were you can store updated content
        private $option_name = 'for_kkranzo_store_recent';
        // this will hold images that were added in the past
        private $old_images = array();
        // set the maximum number of entries to save in recently added
        private $max = 10;
        // name and key of the repeater field
        private $repeater = array(
          'name' => 'my_repeater_field',
          'key' => 'field_XXXXXXXXXXXXX'
        // name and key of the image sub field
        private $image_field = array(
          'name' => 'my_image_field',
          'key' => 'field_XXXXXXXXXXXXX'
        // sub field key => name pairs
        // don't include the image field
        private $sub_fields = array(
          'field_XXXXXXXXXXXXX' => 'my_field_1',
          'field_XXXXXXXXXXXXX' => 'my_field_2'
          // etc...
        // array of all of the sub fields you want to store
        public function __construct() {
          // add a filter that runs before ACF saves values
          add_filter('acf/save_post', array($this, 'before_acf'), 1);
        } // end public function __construct
        public function before_acf($post_id) {
          // this filter will be called before ACF saves new values
          // first make sure the repeater is set in $_POST
          // if not the we don't need to do anything
          if (empty($_POST['acf'][$this->repeater['key']])) {
          // get all the existing images for this post
          // and store them to compare against
          $rows = get_field($this->repeater['name'], $post_id, false); // no formatting
          foreach ($values as $row) {
            $this->old_images[] = $row[$this->image_field['name']];
          } // end foreach rows
          // now loop trough the submitted values to see if there are new ones
          foreach ($_POST['acf'][$this->repeater['key']] as $row) {
            // see if this image is old
            // if it is then continue to the next one
            $image = intval($row[$this->image_field['key']]);
            if (in_array($image, $this->old_images)) {
            // new image - save it
            $new = array(
              'post_id' => $post_id,
              'attachment_id' => $image
            foreach ($this->sub_fields as $key => $name) {
              $new[$name] = $row[$key];
            } // end foreach sub field
            // add the new content to the list
            $this->new_content[] = $new;
          } // end foreach row
          // if there was nothing new then bail
          if (empty($this->new_content)) {
          // get the old option, if anything
          $option = get_options($this->option_name, array());
          // merge the old array onto the new array
          $option = array_merge($this->new_content, $option);
          // strip it down to the max number to save
          $option = array_slice($option, 0, $this->max);
          // save the new option
          update_option($option, $value);
          // now wherever you want to show the new values
          // you can get this option and you have a reference to
          // the post id, the attachment id and any of the other
          // field values you want to display
        } // end public function before_acf
      } // end class for_kkranzo_store_recent