【问题标题】:How to copy data over into my custom database table如何将数据复制到我的自定义数据库表中
【发布时间】: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);
    
} 

wp_postmeta 表

wp_fixtures_results(自定义表)

【问题讨论】:

    标签: wordpress wpallimport


    【解决方案1】:

    到目前为止,这是一个老问题,但我会说 $wpdb->update 是多余的。插入将创建行并存储字段。 $wpdb->insert 应该返回“1”——在删除 postmeta 条目之前检查该值以验证插入是否成功。不需要调用更新。

    【讨论】:

      猜你喜欢
      • 2018-01-26
      • 2016-03-03
      • 1970-01-01
      • 2018-12-08
      • 2012-11-20
      • 2011-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多