Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

<?php
/*
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
add comment

2 Answers

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
add comment

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
add comment

Your Answer

 
discard

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.