WordPress.org

Ready to get started?Download WordPress

Forums

A plugin to Duplicate and Rename a Custom Field (9 posts)

  1. bluebird2
    Member
    Posted 4 years ago #

    I am looking for a plugin to duplicate and rename a custom field in the entire site. Do you know any plugin that can do this?

  2. MichaelH
    Member
    Posted 4 years ago #

    Could use phpMyadmin for something like that, but backup your database first!

    This will update all records where meta_key is 'oldvalue' and replace it with 'newvalue'

    UPDATE wp_postmeta SET meta_key='newvalue' WHERE meta_key='oldvalue' ;

    Of course use the correct table name for your postmeta table if it is not wp_postmeta, and replace newvalue and oldvalue as appropriate.

  3. bluebird2
    Member
    Posted 4 years ago #

    Thanks, but I don't want to replace the older names with new names. I need to create new custom fileds that copy the "value" of the old custom field and save them automatically without me being involved.

  4. MichaelH
    Member
    Posted 4 years ago #

    This seems to work:

    INSERT INTO wp_postmeta (post_id, meta_key, meta_value)(
    SELECT post_id, 'newkey', meta_value
    FROM wp_postmeta
    WHERE meta_key='oldkey'
    )
  5. bluebird2
    Member
    Posted 4 years ago #

    Thanks.

  6. MichaelH
    Member
    Posted 4 years ago #

    Oops, I forgot to warn you to backup your database before proceeding!

  7. romanczar
    Member
    Posted 4 years ago #

    Can I adda question??? How could I use some SQL to change thr PREFIX of my custom fields? e.g. vom "pd_cf_xyz" to "cf_xyz", where xyz is along list of different values.... So I assuem some regex woudl be needed?

  8. kennefu
    Member
    Posted 3 years ago #

    Regarding...

    INSERT INTO wp_postmeta (post_id, meta_key, meta_value)(
    SELECT post_id, 'newkey', meta_value
    FROM wp_postmeta
    WHERE meta_key='oldkey'
    )

    1. Does this copy to the other field within the same post?

    2. The above is within the same table. How would you copy the value from two different tables. Eg. from the post_content to the meta?

    Thanks!

  9. kennefu
    Member
    Posted 3 years ago #

    Alrighty. Figured it out. Rather then approaching it with MySQL, went the PHP route.

    <?php query_posts("showposts=-1&offset=0&category_name=audio"); ?>
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    	<?php update_post_meta(get_the_ID(), 'event', get_the_time('Y-m-d H:i:s')); ?>
    	<?php update_post_meta(get_the_ID(), 'audio_url', get_the_content()); ?>
    	<?php update_post_meta(get_the_ID(), 'speaker', get_the_author()); ?>
    
    	<?php endwhile; ?>
    	<?php endif; ?>
    <?php wp_reset_query(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic