要转换旧的自定义字段,您可以编写一个插件,在激活时使用您的自定义字段名获取所有帖子,并使用handy将其转换为标记wp_set_post_terms
.
首先用字段名和帖子类型设置几个常量。
<?php
/**
* CHANGE THIS! what is your custom field\'s name?
*/
define( \'WPSE29498_FIELD\', \'custom_field_name\' );
/**
* CHANGE THIS! what post type?
*/
define( \'WPSE29498_TYPE\', \'post\' );
然后激活挂钩功能:
<?php
register_activation_hook( __FILE__, \'wpse29498_field_to_tag\' );
function wpse29498_field_to_tag()
{
$posts = get_posts(
array(
\'post_type\' => WPSE29498_TYPE,
\'post_status\' => array( \'publish\', \'draft\', \'pending\', \'future\' ),
\'numberposts\' => -1,
\'meta_key\' => WPSE29498_FIELD
)
);
if( empty( $posts ) ) return;
foreach( $posts as $p )
{
if( $meta = get_post_meta( $p->ID, WPSE29498_FIELD, true ) )
{
wp_set_post_terms( $p->ID, $meta, \'post_tag\', true );
}
}
}
最后,你可以
save_post
并查看是否存在自定义字段值。如果是,请确保它不是一个术语,如果不是,请插入它。
不确定这是您需要的还是一次性转换。
<?php
add_action( \'save_post\', \'wpse29498_save_post\' );
function wpse29498_save_post( $post_id )
{
if( $meta = get_post_meta( $post_id, WPSE29498_FIELD, true ) )
{
// get the current post tag
$terms = wp_get_object_terms( $p->ID, \'post_tag\', \'name\' );
// if our term is already there, bail
if( in_array( $meta, $terms ) ) return;
// add the term if not
wp_set_post_terms( $post_id, $meta, \'post_tag\', true );
}
}
As a plugin.