Monday, January 18, 2010

Adding new tags to the WP term_taxonomy table

This is a continuation of the description I started earlier on inserting categories into your WordPress MySQL database from another MySQL database.

After you get the new tags into wp_terms (and clean up those slugs!!!), you need to add them to the terms taxonomy. Note: WP uses three tables to track Tags, Categories, and Link Categories. They are:
1) wp_terms, which holds the tag names and their URL friendly 'slugs' as well as an id and a useless (since I don't know what it does) field called term_group - which has been '0' for whatever I have tried to add to Categories, Link Categories, and Tags in my experimentation

2) wp_term_taxonomy, which holds the descriptions of your tags as well as which type of tag they are and their hierarchy (for Categories).

3) wp_term_relationships, which relates these tags to your posts and pages.

Let's take a closer look at wp_term_taxonomy:

`term_taxonomy_id` bigint(20) unsigned NOT NULL auto_increment,
`term_id` bigint(20) unsigned NOT NULL default '0',
`taxonomy` varchar(32) NOT NULL default '',
`description` longtext NOT NULL,
`parent` bigint(20) unsigned NOT NULL default '0',
`count` bigint(20) NOT NULL default '0',
PRIMARY KEY (`term_taxonomy_id`),
UNIQUE KEY `term_id_taxonomy` (`term_id`,`taxonomy`),
KEY `taxonomy` (`taxonomy`)


At this point, what we need to do is check to see if all the tags in wp_terms are in wp_term_taxonomy. If not, we need to add their term_id's and fill in some information. Don't get too carried away. At this point, we only want to bother with the term_id and taxonomy columns for post-tags and term_id, taxonomy, description, and parent for Categories. If you choose not to play with Categories, your life is easier. If you like Categories, you have more work to do, but you benefit from being able to further customize your content [I am talking about using themes and presenting category.php]. An excellent discussion of the difference between Categories and Tags is found at the WordPress Codex.

Let's just pretend we have a jumble of stuff and some of it is categories and some is tags. (now I wish I hadn't told you to DELETE TABLE tags when you were through with inserting tags into wp_terms). Go back in time, and imagine that I hadn't. We are going to use TABLE tags here.

more later

Updating wp categories from another mySQL database

If you have a mysql database that is storing your data, but you want to pull that data into wordpress with some key information as tags, you can benefit from the fun I am having. I am sure there is a better way, but this is what I have now.

MySQL and wordpress

If you are like me, and trying to figure out how to merge databases and save yourself time (or buy yourself time if you want to do extra coding but have to get moving NOW), you will benefit from understanding the structure of your WordPress MYSQL database. I have pasted the SQL for the create table command for the table wp-posts in the wp database.

include files using Exec PhP

If you are using Exec-PhP, it is because you have a WordPress blog, and you need to be able to run php code in your pages or posts.

If you try to put this in your file

include(ABSPATH.'yourfile.php');
functionname1();
some php command referring to function();
functionname2();
some php commands referring to function();

assuming that 'yourfile.php' is something like:

function functionname_1() {}
function functionname_2() {}

...you get error messages or something breaks down in different views.
You are probably tired of reading those mysqli error codes, too.

You can save yourself a lot of heartache by following the advice in the readme of the Exec-Php plugin.

They say
As a general rule I would advise to separate all definitions into a file and reference to it by calling require_once(). So the above example would be split into two parts, your article and a file.

Then, you new post or page looks like

require_once(ABSPATH.'yourfile.php');
functionname_fixed();

And, in 'yourfile.php', you have something like:


function functionname_fixed() {
stuff previously included in functionname1();
stuff previously included in functionname2();
echo stuff;
}

What if you need to access another database?

So, I created a database driven website. After some operating time, the site owners wanted to be able to have others comment on the output.

The database is holding information about widgets and then the site's review of the widgets. Owners want other people to be able to add reviews and comments of their own on the "pretty output" pulling the widget data and comments. (I supposed this is kind of like amazon.com's set-up).

I thought that it would be great to use Word Press to simplify adding comments and tags. - I suppose I could have just coded all of that stuff (hmmm.)

I installed wordpress. The first thing necessary was to get the plugin, Exec-PhP, that allows posts to contain php. That way, posts and pages could call the functions and data already written for the previous site.

However, once I made this work, I couldn't figure out how to get each widget review in the original mysql database to show up as a 'post' in the wordpress database.