'Import Data to Custom Database Table Doesn't Without Skipping Other Custom Fields

I am trying to save custom fields into a custom table. With the following code, I can successfully send custom post type(which contains custom fields) into the database. The custom fields no longer go into postmeta table as intended but into the custom table people_data table.

The challenge is that not all custom fields are saved into the people_data table. For example, currently I have about 120 custom posts which are all saved well into the posts table but instead of 120 custom fields in the custom table, I miss out on about 5 posts. So like I have 115 custom fields instead of 120.

I have spent days trying to find the problem but I can't quite tell what I might be doing wrong.

The code

if ($post_type === 'fixture-result') {
    function save_fr_data_to_custom_database_table($post_id)
    {
        // Make wpdb object available.
        global $wpdb;

        // Retrieve value to save.
        $values = get_post_meta($post_id, 'people_data', true);

        // Define target database table.
        $table_name = $wpdb->prefix . "people_data";
        
        // Insert value into database table.
        $wpdb->insert($table_name, array('ID' => $post_id, 'people_data' => $values), array('%d', '%s'));

        // Delete temporary custom field.
        delete_post_meta($post_id, 'people_data');
    }

    add_action('pmxi_saved_post', 'save_fr_data_to_custom_database_table', 10, 1);
}

Thanks.

Helpful link https://www.wpallimport.com/documentation/developers/code-snippets/#import-data-to-custom-database-table-during-record-import



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source