• jon1009

    (@jon1009)


    Hello,

    I seem to be having problems getting the code below to work.

    Basically I am looking to populate 2 custom fields Year and Month I am getting the date from timestamp but for some reason the only the year is 1970 and month January all the time any ideas?

    The code I’am using is below

    add_action( 'save_post', 'setMonth');
    function setMonth($post_id) {
      $post = get_the_ID();
      $slug = 'cruise-dates';
    
      if ( $slug != $_POST['post_type'] ) {
            return;
        }
      if ( !current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
    $date = get_post_meta($post, 'wpcf-start-date', true);
    $month = date('F', $date);
    $year= date("Y",$date);
      update_post_meta($post_id, 'start_month', $month);
      update_post_meta($post_id, 'start_year', $year);
    }

    Cheers

    Jon

Viewing 4 replies - 1 through 4 (of 4 total)
  • catacaustic

    (@catacaustic)

    The date that you’re getting (1970-01-01) is the “default” date when a real date can’t be calculated. There’s reasons for that date, but that’s not the most important thing right now.

    The first thing to do is find out what the value is for $date. If that’s wrong (which is looks like it is) the date calculations won’t ever be correct which is what woudl give you the wrong months value.

    Thread Starter jon1009

    (@jon1009)

    Hello Catacaustic,

    Thanks I has missed the “s” of the $date meta key. Works great now. I cant get it to work on quick edit any ideas how I could implement it with that aswell?

    Cheers

    Jon

    catacaustic

    (@catacaustic)

    I haven’t found a way to make the save work on quick edit because that seems to only pass the “default” WordPress fields back. There might be a way though, so hopefully someone else that knows more could say (hopefully)?

    Thread Starter jon1009

    (@jon1009)

    Thanks, I’ll keep digging.

    Cheers

    Jon

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Updating Custom field on save’ is closed to new replies.