这里有一个简单的插件,其中包含您链接的插件的convert函数,还有一个简单的WordPress cron设置,可以每天运行,只需确保您更改CUSTOM_FIELD_KEY
和TAXONOMY_NAME
至实际值
<?php
/*
Plugin Name: wpse62432
Plugin URI: http://en.bainternet.info
Description: Answer to http://wordpress.stackexchange.com/questions/62432/copy-clone-associate-custom-field-to-custom-taxonomy
Version: 1.0
Author: bainternet
Author URI: http://en.bainternet.info
*/
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 ) )
continue;
$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();
continue;
}
}
$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() {
location_convert(\'CUSTOM_FIELD_KEY\',\'TAXONOMY_NAME\');
}