Home › Forums › Add-ons › Repeater Field › 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:
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?
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.