下面是一个可以放入脚本中的示例,例如:。import.php
在WordPress安装的根目录中。我们迭代所有marina
帖子(或者不管你的CPT是什么)一批50篇(10000篇帖子太多了,无法一次加载),提取联系人详细信息(你需要确保字段名称正确),然后尝试创建一个用户。
<?php
require \'./wp-load.php\';
set_time_limit( 0 );
$query = new WP_Query;
$paged = 1;
while ( true ) {
$query->query([
\'posts_per_page\' => 50,
\'post_type\' => \'marina\',
\'paged\' => $paged,
]);
if ( ! $query->have_posts() ) {
break;
}
foreach ( $query->posts as $post ) {
// Assumed meta fields, change to fit your needs
$name = trim( get_post_meta( $post->ID, \'name\', true ) );
$email = trim( get_post_meta( $post->ID, \'email\', true ) );
// Generate a unique username
$login = $_login = sanitize_key( $name );
$i = 2;
while ( ! $login || username_exists( $login ) ) {
$login = "$_login-" . ++$i;
}
// Name parts
$name_parts = preg_split( \'/ +/\', $name );
// Attempt to create the user
$user_id = wp_insert_user([
\'user_pass\' => wp_generate_password(),
\'user_login\' => $login,
\'user_email\' => $email,
\'display_name\' => $name,
\'first_name\' => array_shift( $name_parts ),
\'last_name\' => implode( \' \', $name_parts ),
\'role\' => \'subscriber\',
]);
if ( ! is_wp_error( $user_id ) ) {
// do something with user ID?
} else {
// something went wrong
}
// Free up memory
clean_post_cache( $post );
}
$paged++;
}
Disclaimer: 此代码仅用于说明目的,我对可能出现的任何错误概不负责。我建议您仔细检查它,提出任何问题,当您有信心运行它时,
backup first!