markratledge
markratledge

Reputation: 17561

MySql and Wordpress query syntax

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".

Upvotes: 1

Views: 2776

Answers (4)

Gipetto
Gipetto

Reputation: 1048

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...

Upvotes: 2

MidnightLightning
MidnightLightning

Reputation: 6928

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')

Upvotes: 0

Phill Price
Phill Price

Reputation: 191

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

Upvotes: 1

Kenneth Reitz
Kenneth Reitz

Reputation: 8846

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_Reference/WP_Query

Upvotes: 2

Related Questions