Updating WordPress database with CSV file – SitePoint


I need to update a wordpress database using a CSV file. My code is generating some errors.

function updatedb( ) {
$myfile = fopen("/test.csv", "r") or die("Unable to open file!");
$csvArray = array_map('str_getcsv', file($myfile));
global $wpdb;

foreach($csvArray as $csvEntry) {

$value = $csvEntry[2]
$datafield = $csvEntry[1]
$sku = $csvEntry[0]
$wpdb->update(
        ppwp_postmeta,
        array(  meta_value => $value,
               ),
        array(  meta_key => $datafield AND
                       post_id => (select post_id
                                              from (select
                                                                     meta_value,
                                                                     post_id
                                                             from ppwp_postmeta
                                                             where meta_key = '_sku') as sku_post
                                                             where meta_value = $sku);
               )
        );
}

    
fclose($myfile);
}

I am using a wordpress cron job to repeat this once an hour.

// Schedule Cron Job Event
function update_stock_cron_job() {
if ( ! wp_next_scheduled( ‘updatedb’ ) ) {
wp_schedule_event( time(), ‘hourly’, ‘updatedb’ );
}
}
add_action( ‘wp’, ‘update_stock_cron_job’ );
?>

I have little experience with PHP or WordPress, so advice on where I’m going wrong would be appreciated.



Source link

قالب وردپرس

Leave a Reply