我有一个网站,每个帖子上都有一个自定义字段(位置)。我想将每篇文章中每个自定义字段值的值更改为标记。
我尝试在这里搜索,但无法获得太多信息。
此处有一些有用的链接:
Plugin to auto convert custom fields to tag
我尝试过使用以下代码,但都不起作用。 add_action(\'save_post\',\'custom_field_add_tags\');
function custom_field_add_tags($post_id) {
$post = get_post($post_id);
//get values of custom fields and put into array
$tag1 = get_post_meta($post_id, \'key_1\', true);
$tag2 = get_post_meta($post_id, \'key_2\', true);
$tag3 = get_post_meta($post_id, \'key_3\', true);
$tags_to_add = array($tag1, $tag2, $tag3);
//now check if tag does not already exist (if no - add tag from custom field)
$add_tags = array();
foreach(get_the_terms($post_id, \'post_tag\') as $term) {
if(!in_array($term->slug, $tags_to_add))
$add_tags[] = $term->slug;
}
if(!empty($add_tags))
wp_add_post_tags($post_id, implode(\',\', $add_tags));
}
Another :
global $wpdb;
$sql = "SELECT post_id , meta_value FROM ".$wpdb->prefix."postmeta WHERE meta_key = \'cp_additional_options\' ";
$res = $wpdb->get_results( $sql ) ;
if( !empty($res)){
foreach( $res as $r ){
$ret = wp_insert_term( $r->meta_value , \'pa_popular-features\' );
if( !$ret->errors ){
$term_id = $ret[\'term_id\'];
wp_set_post_terms( $r->post_id, $term_id, \'pa_popular-features\');
}
}
}
我网站上的文章数量很大。。90000多篇文章。这可以通过一个简单的函数或通过MySQL命令来完成。