Support

Account

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

Solving

Auto-Update Fields based on Expired Date

  • I have a date field (“fl_date”) and basically I would like to automatically update a True/False field (“Date is Expired”), if the date field (“fl_date”) is less than today.

    This should run for all of my custom post types, ideally once per day?

    Is this possible?

    many thanks,
    Andrew

  • This needs to be done using a cron. This has been talked about here but not extensively. You’re best source of information would be looking at how to create cron jobs in WP.

  • ok, but how could this done manually with a function first ?

  • 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 
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Auto-Update Fields based on Expired Date’ is closed to new replies.