Changing Category ID's in Wordpress

Hi all, I’ve run into a problem when developing my wordpress site on my localhost, and then trying to migrate my work to the server install of wordpress. The main problem I have is that the Category ID’s in wordpress are assigned differently between both the localhost and server installs, so I have Categories that don’t have the same ID’s on both of my installs.

This is leading to a template problem where I display categories by ID, and thus the category that my localhost install pulls in is different from the category my server install pulls in (since the ID numbers are different).

Does anyone know of a reliable way to edit the category ID’s in the database so that I can make sure they’re consistent throughout both of my 2 envrionments? I’ve seen elsewhere that the ID’s are assigned in the tables wp_terms and wp_term_taxonomy in the wordpress database. Any ideas?

I would avoid manually trying to change IDs in the database. It would be tedious to change all the relationships in wp_term_relationshps

One alternative would be to use the built in function get_cat_ID() in your theme.

$foodCat = get_cat_ID('food');

Another option would be to just create a settings file in your theme where you set variables to the different category IDs, then include that whenever you need it.

//settings.php file in your theme
$foodCat = '3';
$drinkCat = '5';
//etc
//use those variables in any theme file
include(TEMPLATEPATH . '/settings.php'

PS: Welcome to SitePoint.

Thanks for the suggestions. The only reason why I was trying to change the category ID’s was so that I wouldn’t have to run the risk of having my template break by having someone change the category name without me knowing.

For the second option you suggested, that’s still dependent on the category ID number isn’t it? Or am I missing something?

I guess this is probably the easiest solution, I’ll just have to let everyone involved know that if they change the category names, it’s going to break the template. I had hoped that I could just export the database from my localhost install and overwrite the database on the server, and then have the category ID’s all be the same, but that didn’t seem to make things work right. Is this a solution at all?

In this case, the second solution would work best.

For the second option you suggested, that’s still dependent on the category ID number isn’t it? Or am I missing something?

I guess this is probably the easiest solution, I’ll just have to let everyone involved know that if they change the category names, it’s going to break the template.

You would have to set the variables for each install. Changing the category names in wp admin later would not affect the ids.

I had hoped that I could just export the database from my localhost install and overwrite the database on the server, and then have the category ID’s all be the same, but that didn’t seem to make things work right. Is this a solution at all?

There are some tutorials out there for this. For whatever reason it doesn’t always work right for me. All the ids should be preserved, but sometimes my content gets garbled up.

I see what you were saying about the 2nd option with a settings.php file, that actually makes a lot of sense! And it seems that that’ll be a lot easier to maintain, since I’ll only have two setting.php files to update (localhost and server). Thanks again for the suggestion!

Hello aaronjj,
I have a similar problem with a personal custom theme. It us a lot category ID number in his template.
But I’m not a coder and I don’t really understand what I can really do.
As examle in my template files I have a category: ‘music’ with the ID number of 9
But After installed on my server the ID number of this ‘music’ category is 4.
So in my settings.php what I’m supposed to write ? :

//settings.php file in your theme
$musicCat = '9';
//

And about those variables :

//use those variables in any theme file
include(TEMPLATEPATH . '/settings.php'

Where exactly I’m supposed to paste it in each files ? On the top of all line of all files ? Could you give a precised example ?
Thanks a lot

The settings file doesn’t change anything. If the category ID is 4 on your server, you would set it to 4 in your settings.php file located on the server. On your localhost, set it to 9. The only reason we are doing this is so you can freely make other changes to your theme and upload them without having to worry about category IDs again – just keep the two settings.php files seperate. Wherever you call the music category ID in your theme, change that to use the $musicCat variable

On the first line of the file you are calling category IDs use this


<?php include(TEMPLATEPATH . '/settings.php'); ?>

then change your IDs to use the variables like this


<?php query_posts('cat=' . $musicCat . '&orderby=date&order=ASC'); ?>

I know this is a very old thread, but I found it via a google search for this issue and suspect others will continue to come here…

A slight improvement, I think, to the above suggestion of using variables in an included settings.php file, is to instead define constants for the IDs in your functions.php file.

For example, I have a “video” category which is ID=6 on my localhost. For whatever reason, when do an export/import of the DB from my localhost install to the web server, that ID got changed to ‘7’ - and things broke.

Defining a constant using functions.php is a slight improvement over setting a variable in an included settings.php because you don’t need to add an ‘include’ call to all your theme’s files.

// Set the theme categories
define('VIDEO', 6); /* on locahost this is '6', on client's server this must be updated to '7' */

Then you just reference that constant in all your theme’s files, and you only need to update the functions.php when the ID needs to be changed.