Support

Account

Home Forums General Issues Auto-Update Fields based on Expired Date Reply To: Auto-Update Fields based on Expired Date

  • this is only an example, it will probably need to be adjusted based on your post types/fields.

    
    function update_active_based_on_date() {
      // set the date
      $date = date('YMD');
      // query to get all posts with date < today
      $args = array(
        'post_type' => array(/* list of your post types here */),
        'posts_per_page' => -1,
        'meta_query = array(
          array(
            'key' => 'fl_date',
            'value' => $date,
            'compare' => '<'
          )
        ) // end meta_query
      ); // end $args
      $query = new WP_Query($args);
      // if posts are returned, loop over them and set active flag to false
      if ($query->have_posts()) {
        global $post;
        while ($query->have_posts()) {
          $query->the_post();
          update_field('active_field_name', 0, $post->ID);
        } // end while have_posts
        wp_reset_postdata();
      } // end if have_posts
    } // end function