Support

Account

Home Forums Front-end Issues Delete ACF repeater field entry from backend after date expiry

Solved

Delete ACF repeater field entry from backend after date expiry

  • Hello.
    I am using ACF repeater field to display courses with start_date and end_date.

    Sometimes I have various entries for a single course with different Start_dates and End_dates.

    I want to remove the entries which have expired start_dates from the repeater field so that they don’t show up in the front end with expired Dates.

    Please help.

  • You need to create an acf/load_value filter for the repeater https://www.advancedcustomfields.com/resources/acfload_value/

    When done on the repeater field, $value will be an array or rows with a nested array of the fields in that row. You can loop through the array and remove the ones you don’t want.

  • Thanks John.

    I forgot to mention that I am not so good when it comes to coding. Any chance you can give me a woring example.

    Below is the code I am using to display the fields.

    ?php 
        
    if( have_rows('sub_seminars') ):
    
        while( have_rows('sub_seminars') ) : the_row(); 
            
            ?>
             
    			              
    
    	    <tr>
    	   
            <td style="border: 1px solid #ddd; width: 165px;"><h6><?php the_sub_field('start_date'); ?></h6></td>
            <td style="border: 1px solid #ddd; width: 165px;"><h6><?php the_sub_field('end_date'); ?></h6></td>
    </tr>
    <?php
    			
        endwhile;
    
             endif;
    
            ?>
  • I can give a generic example, but it would be highly dependent on how you are displaying date/time values and this code may not work.

    
    add_filter('acf/load_value/name=repeater_field_name', 'delete_old_courses_by_date');
    function delete_old_courses_by_date($rows, $post_id, $field) {
      if (!is_array($value) || !count($value)) {
        return $value;
      }
      // get the current timestamp
      $now = time();
    
      // set up a new array to hold the values we keep
      $new_value = array();
      foreach ($rows as $row) {
        // the php strtotime() function could fail depending on
        // the return format of the date/time fields
        // this requires a valid date/time format as documented here
        // http://php.net/manual/en/datetime.formats.php
        // if this does not work I probably won't be much help figuring
        // our how to covert your return value to something usable
        $start = strtotime($row['start_date']);
        $end = strtotime($row['end_date']);
        if ($start > $now || $end > $now) {
          $new_value[] = $row;
        }
      }
      return $new_value;
    }
    
    
  • Thanks John
    You are awesome. The code works if I put repeater field name as start_date, all the start_date rows are deleted whether the date is valid or not, it deletes everything, may be the time() function and strtotime() are not in sync, My date output format is default Ymd format, but I would like to know if you please, I have a Nested Structure MY ACF field Name is Sub Seminars ,then I have another sub seminars field inside which is a repeater field containing Start_date and End_date.

    I only need some tweaks in the code that you provided so generously to get it working.

    Thanks for the reply.

  • Not sure why everything is being deleted, like I said, it could be that the date field isn’t valid for strtotime(). The date is probably being converted incorrectly. You may need to make sure that your date is “YYYY-MM-DD’.

    Because you’re using a nested repeater, this also poses an issue. Try using the field_key for the field name for add_filter().

  • Thanks again John.

    My date format is YMD

    The output date in in this format “20170313”

    But this still doesn’t work for me.

  • You probably need to add dashes to the dates before you use strtotime()

    
    $start = $row['start_date'];
    $start = substr($start, 0, 4).'-'.substr($start, 4, 2).'-'.substr($start, 6, 2);
    $start = strtotime($start);
    
  • ACF default format is “Ymd”

    I have used strtotime() before at a different page and it worked fne for me there.

    Something is wrong with it here that i don’t know what

    Thanks for your help.

  • You will also need to look at the return array that you get from the repeater field. I was assuming that your start data and end date are in the same row. If these are in some time of nested repeater then it will be far more complicate.

    You can see what is in any given repeater by using

    
    $repeater = get_field('repeater');
    echo '<pre>'; print_r($repeater); echo '</pre>';
    

    if it’s a nested repeater

    
    $repeater = get_sub_field('repeater');
    echo '<pre>'; print_r($repeater); echo '</pre>';
    
  • add_filter('acf/load_value/name=start_date', 'delete_old_courses_by_date');
    function delete_old_courses_by_date($rows, $post_id, $field) {
      if (!is_array($value) || !count($value)) {
        return $value;
      }
      // get the current date
      $now = date("Ymd");
    
      // set up a new array to hold the values we keep
      $new_value = array();
      foreach ($rows as $row) {
        // the php strtotime() function could fail depending on
        // the return format of the date/time fields
        // this requires a valid date/time format as documented here
        // http://php.net/manual/en/datetime.formats.php
        // if this does not work I probably won't be much help figuring
        // our how to covert your return value to something usable
      $start = date("Ymd", strtotime(get_sub_field('start_date')));
        if ($start > $now) {
          $new_value[] = $row;
        }
      }
      return $new_value;
    }

    Look at this code. Can you figure out what is wrong with it.

    The fields look like this in admin

    ScreenShot

  • $ap = get_post_meta($post->ID,'sub_seminars_0_start_date',true);
            $startdate = date("Ymd", strtotime($ap));
            $todaydate = date("Ymd");
          if(strtotime($todaydate) > strtotime($startdate) && !empty($ap)){
           $del_data = array(
                        'Ref' => 'sub_seminars_0_ref',
                        'Start date' => 'sub_seminars_0_start_date',
                        'End Date' => 'sub_seminars_0_end_date',
                        'Venue' => 'sub_seminars_0_venue',
                        'Fees' => 'sub_seminars_0_fees',
                        'CPE Credits' => 'sub_seminars_0_cpe_credits'
            );
          delete_row('sub_seminars', 1);
        }

    This worked perfectly

  • Hay, have a question – did You put this code in loop or in a add_filter?

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

You must be logged in to reply to this topic.