Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a "author" custom field with the names of author. I want to add some new features to the site so I decided that custom taxonomies will be the best solution. So what I need to do is to get the value of the custom field and pass it to the custom taxonomy called artist. Sure I can do this manually but it is more than 1000 posts so it would be hell.

I guess the solution will be connected with wp_set_object_terms(); function, but nothing I tried worked.

Thank you for any answer.

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

Don't have a time to test it now but something like this should work:

// Change or add your own arguments as needed 
$args = array(
    'post_type'   => 'post', 
    'numberposts' => -1,
    'offset'      => 0
);
$my_posts = get_posts( $args );
if ( $my_posts ) {
    foreach ( $my_posts as $my_post ) {
        $meta = get_post_meta( $my_post->ID, '_name_of_your_custom_field', true );
        if ( ! empty( $meta ) )
            wp_set_post_terms( $my_post->ID, $meta, '_name_of_your_taxonomy');
    }
}

You can wrap it in a function, throw it into your functions.php or make it a plugin. Just be sure you run it just once and not on every page load...

Use the get_posts() arguments to change which posts will be affected. For example you can take advantage of numberposts and offset to split the query into a few smaller ones if your server doesn't feel like processing 1000+ posts at once.

Good luck and let me know if you need more help with any of above.

share|improve this answer
    
I throwed it into functions.php and it works great. Thanks. –  adam Jan 23 '12 at 13:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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