Support

Account

Home Forums General Issues Auto Update Field based on ACF Date Picker field

Solved

Auto Update Field based on ACF Date Picker field

  • Hey everyone,

    i really need some help with my code.

    I have a custom post type named “coupon”. With ACF i added two fields. The first is a Date Picker field named “coupon_valid_till” with my expiration Date. The second one is a true/false field named “is_expired”. I want to change the value of the true/false field to false if the expiration date is reached.

    First i saw this thread:

    https://support.advancedcustomfields.com/forums/topic/auto-update-fields-based-on-expired-date/

    But that didn´t work.

    I then found this pretty similar thread:

    https://support.advancedcustomfields.com/forums/topic/publish-expire-posts-based-on-acf-date-picker-field/

    From the Answers in the Thread, this code should work. But i don´t need to change the Post from public to draft. So i changed this:

      if (($expiredate < $today) && ($expiredate != "")) { // if date is less than today, but also not empty to accomodate the coupons that don't expire
                    $postdata = array(
                        'ID' => $p->ID,
                        'post_status' => 'draft'
                    );
                    wp_update_post($postdata);
                 }      

    to that:

      if (($expiredate < $today) && ($expiredate != "")) { // if date is less than today, but also not empty to accomodate the coupons that don't expire
                  update_field('is_expired', 0, $post->ID);
                 }      

    But it dint´t work either. I managed to setup a cronjob for the code, which gets executed – but nothing happens.

    It would be great if someone could help me.

    Kathi

  • Ok. Problem solved.

    I changed this:

    if (($expiredate < $today) && ($expiredate != "")) { // if date is less than today, but also not empty to accomodate the coupons that don't expire
                    $postdata = array(
                        'ID' => $p->ID,
                        'post_status' => 'draft'
                    );
                    wp_update_post($postdata);
                 }

    To that:

              $expiredate = get_field('coupon_valid_till', $p->ID, false, false); // get the raw date from the db. false, false will convert to Ymd while allowing you to use any date format output choice on the field itself
    
              if (($expiredate < $today) && ($expiredate != "")) {
                  $field_key = "field_634c04868ed81";
                  $value = "1";
                  update_field($field_key, $value, $post->ID);
              }
          } // end while have_posts
          wp_reset_postdata();
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.