【发布时间】:2022-12-03 01:13:47
【问题描述】:
我有以下功能,可将 postmeta 表中的数据插入到自定义数据库表 wp_fixtures_results 中。
我正在使用 WPAll 导入插件操作 pmxi_saved_post。因此代码在导入过程中运行。
该代码的目的是将数据从 wp_postmeta 迁移到 wp_fixtures_results 这是自定义表。
当运行代码进行全新导入时,通常存储在 wp_postmeta 中的数据被移动到自定义表中。这非常有效。
但是,如代码所示,数据仅针对 INSERT 查询运行。使用相同的插件操作,我需要将数据从 postmeta 更新到自定义表中。问题是代码仅适用于 INSERT 查询。如何检查 postmeta 中的数据是否已更改以及在更新数据的导入过程中是否也更新自定义表?
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.
$value = get_post_meta($post_id, 'fixtures_results', true);
// Define target database table.
$table_name = $wpdb->prefix . "fixtures_results";
// Insert value into database table.
$wpdb->insert($table_name, array('ID' => $post_id, 'fixtures_results' => $value), array('%d', '%s'));
// Update query not working - doesn't change data.
$wpdb->update($table_name, array('ID' => $post_id, 'fixtures_results' => $value), array('%d', '%s'));
// Delete temporary custom field.
delete_post_meta($post_id, 'fixtures_results');
}
add_action('pmxi_saved_post', 'save_fr_data_to_custom_database_table', 10, 1);
}
【问题讨论】:
标签: wordpress wpallimport