Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm looking for a solution to "clone or copy or associate" custom fields data to custom taxonomies.

example: Custom field "location" should copy data of that posts custom field to Custom Taxonomy "location"

There is a plugin which Converts custom fields to custom taxonomies on demand, but I would need it's function in cron... automatically on daily basis....

Any solution counts, to adopt this plugin work in cron or a simple code to accomplish this function


share|improve this question

1 Answer 1

Here is a simple plugin with the convert function from the plugin you linked and a simple WordPress cron setup to run daily just make sure you change CUSTOM_FIELD_KEY and TAXONOMY_NAME to the actual values

Plugin Name: wpse62432
Plugin URI:
Description: Answer to
Version: 1.0
Author: bainternet
Author URI:

function Location_convert( $cf_key, $taxonomy ) {
    global $wpdb;

    $rows = $wpdb->get_results( $wpdb->prepare( "
        SELECT post_id, GROUP_CONCAT( meta_value ) as terms
        FROM $wpdb->postmeta
        WHERE meta_key = %s
        GROUP BY post_id
    ", $cf_key ) );

    foreach ( $rows as $row ) {
        $post_id = $row->post_id;
        $terms = explode( ',', $row->terms );
        $terms = (array) apply_filters( 'cft_terms_pre', $terms, $post_id );

        // Convert raw values to term ids
        foreach ( $terms as $i => $term_name ) {
            $term_name = trim( $term_name );

            if ( empty( $term_name ) )

            $term = get_term_by( 'name', $term_name, $taxonomy, ARRAY_A );

            if ( !$term ) {
                $term = wp_insert_term( $term_name, $taxonomy );

                if ( is_wp_error( $term ) ) {
                    self::$errors[$term_name] = $term->get_error_message();

            $terms[ $i ] = (int) $term['term_id'];

        wp_set_object_terms( $row->post_id, $terms, $taxonomy, true );

    return $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = %s", $cf_key ) );

add_action('location_daily_event', 'do_this_daily');

function location_activation() {
    if ( !wp_next_scheduled( 'location_daily_event' ) ) {
        wp_schedule_event( current_time( 'timestamp' ), 'daily', 'location_daily_event');
add_action('wp', 'location_activation');

function do_this_daily() {
share|improve this answer
Thank you for your quick response! ... Will try it.. can I change "daily" to "hourly".. it would be also easier for testing.. If I need more than one field to be converted, should I just copy/paste the whole code or just the last part "add_action..." and just change the CUSTOM_FIELD_KEY','TAXONOMY_NAME'? – Miha Aug 20 '12 at 23:04
yes you can change to hourly, as for another field simply add in the do_this_daily function another call to ` location_convert()` with the second field key. – Bainternet Aug 21 '12 at 6:24

Your Answer


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.