I've several groups by which I have to update/assign parents category to ensure that the default category (uncategorized) becomes parents.

Its super easy to get this done through the category manager, however, I have to do that via script.

The initial source code for wordpress_update_cagetory() is:

 * Aliases wp_insert_category() with minimal args.
* If you want to update only some fields of an existing category, call this
* function with only the new values set inside $catarr.
* @since 2.0.0
* @param array $catarr The 'cat_ID' value is required.  All other keys are optional.
* @return int|bool The ID number of the new or updated Category on success. Zero or FALSE on failure.
function wp_update_category($catarr) {
    $cat_ID = (int) $catarr['cat_ID'];

    if ( isset($catarr['category_parent']) && ($cat_ID == $catarr['category_parent']) )
        return false;

// First, get all of the original fields
$category = get_category($cat_ID, ARRAY_A);

// Escape data pulled from DB.
$category = add_magic_quotes($category);

// Merge old and new fields with new fields overwriting old ones.
$catarr = array_merge($category, $catarr);

return wp_insert_category($catarr);

Which means this appears self-explanatory:

$modifiedCategory = array();

$modifiedCategory['cat_ID'] = 12; // This should be your modified category ID
$modifiedCategory['category_parent'] = 1; // This should be your desired parent category (in my case 1 == Uncategorized)


Repeat the above mentioned for those groups you'll need up-to-date.


Relating towards the problem of category ID retrieval - it appears that category_is available() function should get the job done:

function category_exists($cat_name, $parent = 0) {
$id = is_term($cat_name, 'category', $parent);
if ( is_array($id) )
    $id = $id['term_id'];
return $id;

Which means this should work, however i did not test drive it:

$modifiedCategory['cat_ID'] = category_exists('some category');
$modifiedCategory['category_parent'] = category_exists('Uncategorized');