• Resolved yessoftmk

    (@yessoftmk)


    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:

    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 12 months ago by yessoftmk.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter yessoftmk

    (@yessoftmk)

    in ev_date_end, ev_date_start date time in timestamp format like: 1701601399

    Ashutosh Sharma

    (@ashutosharma97)

    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:

    <?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

    (@yessoftmk)

    thanks for quick help…..

    I use particaly your constructor and I not correct convert time to TS. This right:

    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.