Movable Type: Assign New Parent Category in Bulk while Keeping Existing as Sub-Category

133 Views Asked by At

I have around 2,000 entries on a Movable Type set-up (4.23) that currently have main/parent categories assigned like, say, "apple, pear, banana, grape". I want to keep those as sub-categories and then through some magical, bulk solution assign all of them a new parent category of "Fruit".

It runs on a MySQL database, so my first instinct was to go through there but the the table for each entry doesn't contain a field to modify categories. Those are assigned through a table called mt_placement and it's kind of cuckoo to go in there.

2

There are 2 best solutions below

0
François Nonnenmacher On

I don't know any magic, but Perl and the MT API are enough. Here's how you set an entry category in Perl:

my $place = MT::Placement->new;
$place->entry_id( $entry->id );
$place->blog_id( $entry->blog_id );
$place->category_id( $fruit_cat_id );
$place->is_primary(1);
$place->save
    or die $place->errstr;

You'll need to iterate through the entries, and $fruit_cat_id is the numerical category ID for "Fruit" in the blog in context (categories are per blog i.e. you can have a "Fruit" category in two different blogs but they will have different category IDs).

0
Shmuel Fomberg On

Adding a parent category: your MT is pretty old. In new MTs, you can move a category to be a sub-category using drag-n-drop interface.

You probably don't have this, (otherwise you wouldn't ask) so you will have to mass with the database. Create (using MT interface) a new category named 'fruit', write down its id.
Then in the database, in the table "mt_category", for every category that you want to be its subcat, set the "category_parent_id" value to the id of the fruit category.

Next to adding the category to each entry that have one of its subs: you need to add rows to "mt_placement", as Francios said. You can do it using a Perl script, (iterating over the existing placements and creating new ones) or using some fancy SQL. (select inside an insert? I don't know anything about it)