Support » Fixing WordPress » Adding multiple categories into one

  • Well to make it simple I have around 150 categories on WP, however I would somehow like to be able to add all those to one main category (while still keeping them in the original category), is this possible with a plugin at all? I don’t mean going through every post either because there is aroudn 10,000 posts and that would take quite some time.

Viewing 12 replies - 1 through 12 (of 12 total)
  • add_action ('init', 'general_parent_cat');
    function general_parent_cat() {
        $parent_cat_id = 1; // change 1 to the ID of the parent category
        $categories=get_categories(array('exclude' => $parent_cat_id, 'taxonomy' => 'category'));
        foreach($categories as $category) {
            // only if a category doesn't have a parent
    	if (!$category->category_parent) {
                wp_update_term($category->term_id, 'category', array('parent' => $parent_cat_id));

    Tested. It will save existing cats hierarchy and add one parent cat to all top cats.
    How to use it.
    Change 1 to the ID of a category that should be a general parent. Add changed code to the functions.php and save it. Check the hierarchy of categories on the Posts / Categories page. Then delete this code and save again functions.php. Otherwise it will execute forever 🙂
    This category will not be assigned to posts, but it’s archive will contain all posts.
    Do you need to assign this cat to all posts?

    If you need to move all posts from many categories into one –

    Just notice that after the moving posts you will get 10,000 post revisions if you didn’t disable saving revisions. But it’s easy to remove them.

    I’d like for the main category I am adding these posts to, to be able to show up above the posts themselves.

    (like for example I have my post, then a toolbar on the bottom that names what category the post is in)

    Will that do it with this function?

    This function doesn’t assign new category to all posts, but makes it parent. So all posts will present in this category archive, but posts will not have a link to this cat in the cat-list at the bottom.
    To clarify, do you want
    to add new one category,
    make it a parent for all others and to save old categories assigned to posts,
    assign new cat to all posts,
    or you want to assign all posts to only one category and remove old categories ?

    My goal is to assign a new category to all posts within 150 existing categories, so I’d still like to keep the old categories too.

    So, this category shouldn’t be parent for other cats, just a new category that assigned to all posts?


    And another question: assigned to all posts or you have some exceptions like another 100 categories with 5000 posts those must stay without changes? 😉

    There is one category with around 500 posts that I don’t want to apply to, so yea… only one category though.

    Hey vjpo, do you know of a solution? I greatly appreciate your help 🙂

    I am attempting to find a solution, but can’t get it work ;/ Need some time for it…

    add_action('init', 'assign_category_to_multiple_posts');
    function assign_category_to_multiple_posts() {
    	$add_category = 1;
    	$exlude_posts_in_category = 2;
    	$the_query = new WP_Query;
    	$posts = $the_query->query(array('post_type'=>'post', 'nopaging'=>'true', 'category__not_in' => $exlude_posts_in_category));
    	foreach($posts as $post) {
    		wp_set_object_terms($post->ID, $add_category, 'category', true);

    Ok! It works )
    $add_category = 1; change 1 to the ID of category that you wish to add
    $exlude_posts_in_category = 2; change 2 to the ID of category of posts that you don’t want to apply to, or array(2, 3) for few cats.
    Add the code to the functions.php and save,
    open Posts list in new window, may be you will need to reload it a couple of times to see a result.
    Then delete the code from functions.php

    Make a database backup before!

    At least I found the plugin for this task. It has the option “Preserve existing categories for selected posts”. So you can choose 🙂 The difference – with the pugin you need to unselect some posts manually.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Adding multiple categories into one’ is closed to new replies.