views:

559

answers:

4

I'm trying to learn MySQL queries for use with Wordpress....

What I do know is this query, which gives all instances of the custom field "description" the value of "test" if the field is NULL

UPDATE `wp_postmeta` SET `meta_value` = 'test' WHERE `meta_key` LIKE 'description' and `meta_value` is null

What I need to do is change that so I can write to a category; categories are stored in wp_term, like this: WHERE term_id = '3'

But, this throws a syntax error:

UPDATE `wp_postmeta` SET `meta_value` = 'test' WHERE `meta_key` LIKE 'description' and `meta_value` is null WHERE term_id = '3'

So I'm doing something wrong; I'm thinking the syntax that is involved with categories is much more complex than tagging in another "WHERE".

+2  A: 

You shouldn't query SQL directly. There is an abstraction layer built into wordpress.

You should be using the wp_query function:

http://codex.wordpress.org/Function%5FReference/WP%5FQuery

Kenneth Reitz
I'll move to using Wordpress functions after I learn a bit more within PhpMyAdmin, but I might move sooner....
songdogtech
+1  A: 

This might be it ?

UPDATE wp_postmeta SET meta_value = 'test' WHERE meta_key LIKE 'description' and meta_value is null AND term_id = '3'

On mine I have problems with the ` as well;

there's also the update_post_meta() function you should look into

http://codex.wordpress.org/Function_Reference/update_post_meta

Phill Price
Hmm.. throws a `#1054 - Unknown column 'term_id' in 'where clause' `FWIW, I'm trying to write to all posts/pages with that particular category, and `update_post_meta` requires a post ID. But, with a new WP query loop, I suppose I could select all posts. I figured this was more complicated that first glance....
songdogtech
A: 

Given that you should use the WordPress function if you're operating within the WordPress engine, to make a valid SQL statement, turn your second "WHERE" into an "and" you'd have a valid SQL query. You're describing a three-part WHERE statement, which could be shown as:

WHERE (`meta_key` LIKE 'description') AND (`meta_value` is null) AND (`term_id` = '3')
MidnightLightning
I'm working in PhpMyAdmin right now and will move to Wordpress functions, but maybe I should jump to WordPress functions sooner, as I want to use these queries in possibly a Wordpress plugin, anyway.Your example throws a `#1054 - Unknown column 'term_id' in 'where clause' `
songdogtech
+1  A: 

This is off the top of my head. What you want to use to update a category is wp_update_term

wp_update_term(3,'category',array(
    'name' => 'new name',
    'description' => 'new description'
));

You don't have to include both name and description, just include that one that's changing. wp_update_term will merge this new data in to the old data so that only what you give it is what is changed.

Now that I've read the post again, I'm not sure if this answers it. I can't honestly tell what the goal of your SQL is...

Gipetto
Thanks, good points, I'm going to have to learn what I need this way, using `wp_update_term` rather than directly talking to the DB with PhpMyAdmin.What I want to do is be able to add/change/delete custom fields and values in bulk, from WordPress, so it makes sense to go at it from that level.I need to find out how to get `wp_update_term` to let me change all posts, rather than one at a time `from post_id` as the documents say.
songdogtech