Problem: There should be a bunch of rows being inserted into the database via $wpdb->insert, but no new rows are created.
Background:
- I’ve checked the query in PhpMyAdmin and it returns 178 rows.
- This is for a WordPress plugin with a function that fires on ‘init’.
- Have tried various var_dump’s and still variables $object_id and $taxo_id appear to be empty.
Please help!
<?php
function cb_t2c_cat_updater () {
global $wpdb;
$prefix = $wpdb->prefix;
//This is a truncated version of the valid SQL query, returns 178 rows
$cb_t2c_cat_update = $wpdb->get_results("SELECT
".$prefix."associations.object_id as 'object_id', ".$prefix."associations.term_taxonomy_id as 'taxo_id', ".$prefix."associations.term_id as 'term_id', ".$prefix."associations.cat_ID as 'cat_ID'
FROM ...
WHERE...
ORDER BY object_id ASC, term_id ASC, cat_ID ASC",ARRAY_A);
foreach ($cb_t2c_cat_update as $values) {
$table = $prefix . 'term_relationships';
$object_id = $values['object_id'];
$taxo_id = $values['taxo_id'];
$wpdb->insert(
$table,
array(
'object_id' => $object_id,
'term_taxonomy_id' => $taxo_id,
'term_order' => 0
),
array(
'%d',
'%d',
'%d',
)
);
}
}
add_action('init', 'cb_t2c_cat_updater');
?>
Have a working solution.
Made the variables integers through:
and then passed these into a query like: