A
A
Alex2018-05-28 17:29:25
WordPress
Alex, 2018-05-28 17:29:25

How to update the fields on a schedule, once at a given time?

There is a custom record type in which 14 records are stored, (the first week from Mon to Sun and the second week from Mon to Sun) each record has a "week number" field - 1 or 2, and using acf a repeater was created, which in the line has the name and 2 numeric fields, the first is constantly changing, and the second field is the field to which the value of the first field must be brought according to the schedule (for example, once an hour). I use wp_cron, it turned out something like this.

$parameters = array(
  'post_type_schedule' => 'schedule'
);
if( !wp_next_scheduled( 'hook_update_schedule_everyweek',  $parameters ) )
  wp_schedule_event( time(), 'user_everyweek', 'hook_update_schedule_everyweek', $parameters );

add_action( 'hook_update_schedule_everyweek', 'update_schedule', 99, 1 );

function update_schedule( $post_type_schedule) {
  // здесь получаю 7 из 14 постов где номер недели предыдущий (в данном случае 1)
  $prev_week = 1;
  $posts_last_week = new WP_Query(array(
    'post_type' => 'schedule',
    'posts_per_page' => -1,
    'meta_query' => array(
      array(
        'key' => 'number-week',
        'value' => $prev_week
      )
    )
  ));
  // дальше нужно каким то образом пробежаться по массиву всех строк по всем постам и заменить значение
   // взяв из count в той же строке и записать в remain
  foreach ($posts_last_week->$posts as $post_i => $post) {
    setup_postdata($post);
    while (have_rows('schedules', $post->ID)) {
      the_row();
      $count = get_sub_field('count');
      update_sub_field('remain', $count);
    }
  } 
  wp_reset_postdata();
}

I use the acf plugin, either I didn’t figure out how to update the fields in the repeater, or it’s impossible to do this at all using its methods. Tell me how can this be done?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question