WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm testing a script to create a database insert. Is this the correct syntax for the insert or do I need to obtain a reference to global $wpdb and use that?

Plugin Name: Test Database Insert

function test_db_insert()
INSERT wp_terms(term_id, 'name', slug) VALUES (1, 'test', 'test');
INSERT wp_term_taxonomy(term_taxonomy_id, term_id, taxonomy, parent) VALUES (1, 1, 'category', 0);
INSERT wp_term_relationships(object_id, term_taxonomy_id, term_order) VALUES (1, 1, 0);

register_activation_hook(__FILE__, 'test_db_insert');
share|improve this question
up vote 3 down vote accepted

This seems like raw SQL that won't make sense to PHP at all.

You can run this queries, as raw queries using $wpdb->query() but it's more proper to use its $wpdb->insert() method.

See wpdb Class > INSERT rows in Codex.

share|improve this answer

You shouldn't need to use SQL at all for that. Replace it with:

wp_set_object_terms(1, 'test', 'taxonomy_name');

…where taxonomy_name is the name of the taxonomy.

share|improve this answer
Seems to be the simplest, but I'm not sure it will do exactly what I want. Can I define the term_id of the new term (assuming it does not already exist)? – Scott B Dec 14 '10 at 20:19
No but — at the risk of sounding glib! — why would you want to? Surely all that matters is that the ID is unique, since it's never actually seen by anyone? – Rob Miller Dec 15 '10 at 10:13
I'm keying off cat_id = 1 to hide the uncategorized and all its children, since i use these for a specific purpose. – Scott B Dec 26 '10 at 17:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.