Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 741679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:39:38+00:00 2026-05-14T08:39:38+00:00

I have several categories in which I need to update/assign the parent category so

  • 0

I have several categories in which I need to update/assign the parent category so that the default category (uncategorized) becomes the parent.

Its very easy to do this via the category manager, however, I need to do this via script.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:39:39+00:00Added an answer on May 14, 2026 at 8:39 am

    The original source code for wp_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);
    }
    

    So this seems 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)
    
    wp_update_category($modifiedCategory);
    

    Repeat the above for all categories you need updated.

    UPDATE:

    Pertaining to the problem of category ID retrieval – it seems that category_exists() function should do the job:

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

    So this should work, but I didn’t test it:

    $modifiedCategory['cat_ID'] = category_exists('some category');
    $modifiedCategory['category_parent'] = category_exists('Uncategorized');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several categories that I use in my Grails plugin. e.g., class Foo
I have a product database with several product categories. Each category has a number
I have a screen which allows users to add several categories at once. They
I have a real estate website that has several categories. And i want the
I have this ViewModel to represent each Category and it's several Sub-Categories in my
I have several tables, books, bookcategories, categories bookcategories is a join table to allow
I have several HTML elements (buttons) that fire the same JQuery AJAX request. When
I have several Delphi programs that maintain connections to a database (some Oracle, some
I have several different numbers in a group that range in sizes and would
I have several xml files that are formated this way: <ROOT> <OBJECT> <identity> <id>123</id>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.