WordPress action save_post not working properly on publish

  Kiến thức lập trình

I have a custom post type named “map” created with Pods. Each time a map post is created, updated or trash, we are updating a JSON file with data.

Our code is working fine when we UPDATE or TRASH the post, but is not when we PUBLISH the post. When we publish the post, the function runs but can’t pull the data properly from the custom data fields to fill the JSON, it returns Jan 1, 1970 each time but the image url is fine. Then if we click UPDATE after the creation of the post, the function is called again and now all data in the JSON are populated correctly.

Any idea how I can fix this glitch in my code?

function generate_my_json($post_ID) {
    // Check if the post type is 'satellite_collars_ma'
    if (get_post_type($post_ID) !== 'map') {
        return;
    }
   
    $map_entries = array();

    $args = array(
        'post_type' => 'map',
        'posts_per_page' => -1,
    );

    $map_query = new WP_Query($args);

    while ($map_query->have_posts()) {
        $map_query->the_post();
    
        $map_date = get_post_meta(get_the_ID(), 'map_date', true);

        // Customize the date format if needed
        $start_date = array(
            'month' => date('m', strtotime($map_date)),
            'day' => date('d', strtotime($map_date)),
            'year' => date('Y', strtotime($map_date)),
        );
        
        // Customize the featured image URL based on your setup
        $featured_image_url = get_the_post_thumbnail_url(get_the_ID(), 'full');
    
        // Customize this part to structure your JSON as needed
        $entry_data = array(
            'media' => array(
                'url' => $featured_image_url,
            ),
            'start_date' => $start_date ,
        );

        $map_entries[] = $entry_data;
    }

    wp_reset_postdata();

    $json_data = json_encode(array('events' => $map_entries), JSON_PRETTY_PRINT);

    // Customize the path where you want to save the JSON file
    $json_file_path = ABSPATH . 'wp-content/map-json/my-feed.json';

    file_put_contents($json_file_path, $json_data);
}

add_action('save_post_satellite_collars_ma', 'generate_my_json', 10, 1);
add_action('wp_trash_post', 'generate_my_json', 10, 1);
add_action('untrashed_post', 'generate_my_json', 10, 1);

This the content of the JSON when I PUBLISH the post:

{
    "events": [
        {
            "media": {
                "url": "https://mysite.com/wp-content/uploads/2024/05/20240516-map.jpg"
            },
            "start_date": {
                "month": "01",
                "day": "01",
                "year": "1970"
            }
        },
    ]
}

This the content of the JSON when I UPDATE the post:

{
    "events": [
        {
            "media": {
                "url": "https://mysite.com/wp-content/uploads/2024/05/20240516-map.jpg"
            },
            "start_date": {
                "month": "05",
                "day": "22",
                "year": "2024"
            }
        },
    ]
}

LEAVE A COMMENT