5

I have 3000 posts with that custom field name 'refer' and i need to change it to 'ref' for theme needs, instead of doing it manually i need a query or technique to change them all.

Question 1
I am trying to rename a custom field, from 'refer' to ref. what is the query needed to rename a custom filed. it would be better if its done using function.php(instead of doing in phpmyadmin).

Question 2
I also have a lot of unwanted custom fields which are need to be deleted, how should i go about. help me out guys.

3 Answers 3

12

I found the answer

UPDATE `wp_postmeta` SET `meta_key` = 'ref' WHERE `meta_key` = 'refer'

use this part in your SQL

Reference

2
  • Ohh, you never mentioned you got 3000 posts with that custom field! Glad you found the answer! Commented Oct 16, 2011 at 7:25
  • FYI: No need for the backticks around the table/column names.
    – t31os
    Commented Oct 16, 2011 at 10:45
0

If you're trying to delete a post's custom field, then please edit your post, select 'Custom Fields' from the screen options, and scroll to the bottom, below the post editor. You'll find all your custom fields listed out there, you can edit their name, and change their values too. But ONLY those which you've added while adding / editing the post. Not the ones that WP creates for each post. DO NOT DELETE THE CUSTOM FIELDS WHICH YOU HAVEN'T ADDED!

1
  • i already have 3000 posts with that custom field name 'refer' and i need to change it to 'ref' for theme needs, instead of doing it manually i need a query or technique to change them all.
    – Ezhil
    Commented Oct 16, 2011 at 4:47
0

An cleaner version of @t31os's answer :

global $wpdb;
$querystr = $wpdb->prepare( "UPDATE $wpdb->postmeta SET meta_key = '%s' WHERE meta_key = '%s'",'new','old');
$result = $wpdb->get_results ( $querystr );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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