设置如下:我有带有自定义字段的帖子:actor = \'tom hanks\'
对于高级管理,我创建了一个自定义的帖子类型(actors),并在actors分类中添加了“tomhanks”作为帖子。
通过插件“高级自定义字段”,我将自定义分类法(actors)连接到默认帖子,这样我就可以在常规帖子页面上选择“TomHanks”。
这可以手动完成(检查自定义字段值并单击相应的actors类别),但有超过1500篇文章和许多其他类似的自定义字段值。
您知道如何通过SQL查询或函数来解决这个问题吗?
在一行中:get value from customfield \'actors\' and move to taxonomy(\'actors\') for that post.自定义字段值可以用作分类法slug。
我认为这也是可能的wp_update_post 函数,但我不知道如何调用所有帖子、自定义字段和分类法。
如果有人能帮我查询mysql或类似的数据库,你一定会让我高兴的:)
编辑:我想wp_update_post
是完成这项任务的好方法。
// get all post IDs
$post_ids = get_posts(array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'numberposts\' => -1, // get all posts.
\'fields\' => \'ids\', // Only get post IDs
));
// info: custom field \'actor\' = \'Tom Hanks\'
// Update all posts
foreach ($post_ids as $id) {
$excerpt = get_post_meta($id, \'excerpt\', true); // for testing purpose I added a customfield \'excerpt\'
// info: custom field \'actor\' = \'Tom Hanks\'
$customfield_value = get_post_meta($id, \'actor\', true);
// change the custom field values to slug format: \'Tom Hanks\' > \'tom-hanks\'
$customfield_value = sanitize_title_with_dashes($customfield_value);
$post = array(
\'ID\' => $id, // select all posts
\'post_excerpt\' => $excerpt, // update the excerpt field with the customfield \'excerpt\'
\'tax_input\' => array(
\'actor\' => array(
$customfield_value
)
)
);
wp_update_post( $post );
// delete the old post meta after we moved it to the taxononmy
//delete_post_meta($id, \'actor\');
}
SO网友:s_ha_dum
您的想法是正确的,但是您错误地使用了一些函数和函数参数。
// get all post IDs
$post_ids = get_posts(
array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'numberposts\' => -1, // get all posts.
\'fields\' => \'ids\', // Only get post IDs
)
);
// info: custom field \'actor\' = \'Tom Hanks\'
$customfield_value = get_post_meta($post_ids, \'actor\', true);
// change the custom field values to slug format: \'Tom Hanks\' > \'tom-hanks\'
$customfield_value = sanitize_title_with_dashes($customfield_value);
// Update all posts
foreach ($post_ids as $id) {
$post = array(
\'ID\' => $id, // select all posts - not sure if that works with the array from $post_ids.
\'tax_input\' => array(
\'actor\' => array(
$customfield_value
)
)
);
// Update the post into the database
wp_update_post( $post );
// delete the old post meta after we moved it to the taxononmy
delete_post_meta($id, \'actor\');
}
那就是
untested.
Do not run 在生产数据库上进行测试,而不首先在副本上进行测试。要非常非常小心,尤其是在删除帖子元时。这是不可逆的。