Custom field values to taxonomy terms

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.

Read More

Thank you for any answer.

Related posts

Leave a Reply

1 comment

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