Title: Modify meta data when add post
Last modified: December 3, 2023

---

# Modify meta data when add post

 *  Resolved [yessoftmk](https://wordpress.org/support/users/yessoftmk/)
 * (@yessoftmk)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/modify-meta-data-when-add-post/)
 * Hi, please tell me what I’m doing wrong. On my site automatically adding events(
   CPT) from facebook with two meta fields:
    - ev_date_start
    - ev_date_end
 * all ok, automatically adding events with all necessary fields but some time in
   source (facebook) ev_date_end is empty…. its broke post…
   I came up with the following
   solution:
 *     ```wp-block-code
       add_action( 'added_post_meta', 'save_post_event', 10, 4 );
       function save_post_event( $meta_id, $post_id, $meta_key, $meta_value ){
   
       if ( get_post_type( $post_id ) != 'event' ){
       return;
       }
   
       if ( $meta_key == 'ev_date_end')
       {
       if ( $meta_value == '')
       {
   
       $start = strtotime(get_post_meta ( $post_id, "ev_date_start", true)+ 3600);
   
       update_post_meta( $post_id, 'ev_date_start', strtotime($start) );
       }
       }
       }
       ```
   
 * but with this code both my field (ev_date_start, ev_date_end) are empty.
    -  This topic was modified 2 years, 6 months ago by [yessoftmk](https://wordpress.org/support/users/yessoftmk/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fmodify-meta-data-when-add-post%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [yessoftmk](https://wordpress.org/support/users/yessoftmk/)
 * (@yessoftmk)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/modify-meta-data-when-add-post/#post-17248520)
 * in ev_date_end, ev_date_start date time in timestamp format like: 1701601399
 *  [Ashutosh Sharma](https://wordpress.org/support/users/ashutosharma97/)
 * (@ashutosharma97)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/modify-meta-data-when-add-post/#post-17248570)
 * Your code seems to be incorrect. If the meta values are supposed to be timestamps,
   you are not supposed to use `strtotime()` and after adding an hour to the event
   start timestamp, you are again setting the same meta, i.e., `ev_date_start` instead
   of `ev_date_end`.
 * Haven’t tested, but you need something like this:
 *     ```wp-block-code
       <?php
   
       function validate_fb_post_meta($meta_id, $post_id, $meta_key, $meta_value) {
           if ($meta_key !== 'ev_date_end') {
               return; // Not our meta key
           }
           if (get_post_type($post_id) !== 'event') {
               return; // Not our post type
           }
           if (empty($meta_value)) {
               $start_date = get_post_meta($post_id, 'ev_date_start', true);
               if (empty($start_date)) {
                   // Add your logic if even ev_date_start is empty
               } else {
                   $start_ts = filter_var($start_date, FILTER_VALIDATE_INT);
                   if ($start_ts === false) {
                       // Add your logic if ev_date_start value is not a valid integer. TS are integer values
                   } else {
                       $end_date = $start_date + 3600;
                       update_post_meta($post_id, 'ev_date_end', strtotime($end_date));
                   }
               }
           }
       }
       add_action('added_post_meta', 'validate_fb_post_meta', 10, 4);
       ```
   
 *  Thread Starter [yessoftmk](https://wordpress.org/support/users/yessoftmk/)
 * (@yessoftmk)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/modify-meta-data-when-add-post/#post-17249778)
 * thanks for quick help…..
 * I use particaly your constructor and I not correct convert time to TS. This right:
 *     ```wp-block-code
       if (empty($meta_value)) {
               $start_date = get_post_meta($post_id, 'ev_date_start', true);
               $end_date = strtotime('+ 1 hour', $start_date); 
               update_post_meta($post_id, 'ev_date_end', $end_date);
       ```
   
 * now works, thanks.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Modify meta data when add post’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 2 participants
 * Last reply from: [yessoftmk](https://wordpress.org/support/users/yessoftmk/)
 * Last activity: [2 years, 6 months ago](https://wordpress.org/support/topic/modify-meta-data-when-add-post/#post-17249778)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
