1

I'm trying to add a custom field of event_month when a post is published or saved. I'm using the save_post action and getting the contents of a custom field containing the date and trying to store this in a separate custom field with just the month. This works perfectly when saving a post that has already been created. I've shown my code below.

add_action('save_post', 'update_event_date');

function update_event_date($post_id){

    $post_type = get_post_type($post_id);
    $event_datee = get_post_meta($post_id, '_EventStartDate', true);

    if ($post_type == 'tribe_events'){

        $month = date("m",strtotime($event_datee));
        update_post_meta($post_id, 'event_month', $month);

    }

}

The problem arises when creating a new post. I think this is because the action fires before the _EventStartDate meta has been created and therefore the month can't be taken from this.

The hook is firing correctly and as intended when saving/updating a post but doesn't correctly get the month from the meta when creating a new post.

I'd really appreciate it if someone could provide me with some guidance.

3
  • save_post is an action triggered whenever a post or page is created or updated Commented Aug 24, 2017 at 9:07
  • Possible duplicate of Custom post type save_post action not firing Commented Aug 24, 2017 at 9:09
  • I don't think I understand your problem... From my understanding, it seems like you try to get a post meta from a post that has not been created yet... Therefore, that would be normal you don't get its meta? Or your problem is it that you don't get the meta box in the posts you're creating? Or is that it doesn't save the meta box value when you're creating a new post?
    – ClemC
    Commented Aug 24, 2017 at 12:32

2 Answers 2

1

You can hook into post_updated action, if you want to access the post's data after it's been published. This hook passes the post's ID, inundated post object, and updated post object.

add_action( 'post_updated', 'update_event_date', 10, 3 );
function update_event_date( $post_id, $post_after, $post_before ){

    $post_type = get_post_type( $post_id );
    $event_datee = get_post_meta( $post_id, '_EventStartDate', true );

    if ( $post_type == 'tribe_events' ) {

        $month = date( "m",strtotime( $event_datee ) );
        update_post_meta( $post_id, 'event_month', $month );

    }

}
1
  • 1
    Thanks Jack. I found a solution before I read this using $_POST to grab the value I needed, but I'll be sure to try this method again.
    – Neil K
    Commented Aug 24, 2017 at 15:11
0

Use pre_post_update instead of save_post

Full explanation here

2
  • I'm still getting the same issue. It works perfectly when saving or updating a post, but when creating a new event. It just sets the event_date as 01, rather than the actual month of the event. However, when I then hit update post, it sets the event_month correctly
    – Neil K
    Commented Aug 24, 2017 at 9:17
  • Check if your pre_post_update hook fires first. For example, put global $post->ID to any rewritable file on your filesystem. Commented Aug 24, 2017 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.