Support

Account

Home Forums Add-ons Repeater Field Delete repeater`s sub_field after expired date

Solving

Delete repeater`s sub_field after expired date

  • Hello, i have repeater (‘pasirinkti_datas’), and in this repeater are stored datepicker (‘keliones_datos’) Y-m-d format.

    I need to do a function that deletes repeater row if datepicker date is lower than todays date

  • To do this you need to use a CRON that:

    1. Queries all the posts
    2. Loops over the posts
    3. Loops over the repeater on each post (in reverse – SEE NOTE)
    4. Checks the date in each row
    5. Deletes expired rows using delete_row()

    Notes, you must delete the rows in revers order and you cannot use an have_rows loop to loop over the rows. The reason is this, lets say that you delete row 2, row 3 then becomes row 2 and the next iteration of the loop will look at row 3, which was row 4 before the deletion and the loop skips what was row 3 because it is now row 2.

    
    $repeater = get_field('repeater_name');
    // $repeater is an array of rows
    $count = count($repeater);
    for ($row=$count; $row>0; $row--) {
      if (//DATE EXPIRED//) {
        delete_row('repeater_name', $row);
      }
    }
    
  • Thank you for information, i have more questions now:
    How i can querry and loop over all woocommerce products in functions.php?

    I created cronjob, made it run one time per day:

    
    add_action( 'triname_datas', 'triname_datas_func' );
    function triname_datas_func() {
      $today = date('Y-m-d');
      $repeater = get_field('pasirinkti_datas');
    // $repeater is an array of rows
    $count = count($repeater);
    for ($row=$count; $row>0; $row--) {
      if (get_sub_field('keliones_datos', $repeater) >= $today) {
        delete_row('pasirinkti_datas', $row);
      }
    }
    }
    

    But it is not working, i need to query and loop through products.

  • https://developer.wordpress.org/reference/classes/wp_query/

    You need to do a query to get all of the posts you want to look at and then use a have_posts() loop on that query. You will need to do global $post; in your code before that loop.

  • Got it. But now it deletes all the rows, i think i write bad IF function or delete_row

    
      $args = array( 
        'post_type' => 'product', 
        'post_status' => 'publish',
        'posts_per_page'  => -1,
      );
    
      $listings = new WP_Query( $args );
      $count = 0;
      $today = date( 'Y-m-d' );
      global $product;
    
    // The Loop
    if ( $listings->have_posts() ) {
    	echo '<ul>';
    	while ( $listings->have_posts() ) {
    		$listings->the_post();
    		$count = 0;
        $repeater = get_field('pasirinkti_datas',$listings->ID);
        if (is_array($repeater)) {
          $count = count($repeater);
        }
    for ($row=$count; $row>0; $row--) {
      if (get_sub_field('keliones_datos',$listings->ID) <= $today ) {
        delete_row('pasirinkti_datas', $row,$listings->ID);
      }
    }
    echo '<li>' . get_the_title() .$count . '</li>';
    	}
    	echo '</ul>';
    } else {
    	// no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    
  • 
     $args = array( 
        'post_type' => 'product', 
        'post_status' => 'publish',
        'posts_per_page'  => -1,
      );
    
      $listings = new WP_Query( $args );
      $count = 0;
      $today = date( 'Ymd' );
      global $product;
    
    // The Loop
    if ( $listings->have_posts() ) {
        while ( $listings->have_posts() ) {
            $listings->the_post();
            $count = 0;
        $repeater = get_field('pasirinkti_datas',$listings->ID);
        if (is_array($repeater)) {
          $count = count($repeater);
        }
    
        echo $count. '</br>';
        if( have_rows('pasirinkti_datas', $listings->ID) ):
          while( have_rows('pasirinkti_datas', $listings->ID) ) : the_row();
          
              $value = get_sub_field('keliones_datos', $listings->ID);
              if($value < $today) {
                  $row = get_row_index();
                  delete_row('pasirinkti_datas', $row, $listings->ID);
              }
          endwhile;
      endif;
        }
    } else {
        // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    

    This is the working code i guess?

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

You must be logged in to reply to this topic.