Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress & SQL: Adding a custom field to all posts

I need a SQL query that I can run in PHPmyadmin that adds a custom field (with a value) to all existing posts. Can anyone help? Is this even possible?

like image 976
Alex Avatar asked Jun 29 '10 11:06

Alex


2 Answers

You can do this by inserting into the postmeta table using a select query on the posts table.

In the example below I use wp_ as my table prefix, your setup may differ.

insert into wp_postmeta (post_id, meta_key, meta_value) 
select ID, 'my-key', 'my-value' from wp_posts where post_type = 'post';
like image 93
windyjonas Avatar answered Sep 19 '22 13:09

windyjonas


Yes, this is possible, but it is inadvisable. Your data could be lost the next time you upgrade. If you're not planning on upgrading, then you're leaving gaping security holes in your site. The recommended way to do this is to use the postmeta table. This is what it exists for.

EDIT

Now that I better understand the question, ignore the part above. See comments for more details.

like image 40
John P Bloch Avatar answered Sep 19 '22 13:09

John P Bloch