<?php
$current_blog_id = get_current_blog_id();
$log_file = 'log/sql.log'; // 日志文件路径
wp_reset_query();
// print_r("<pre>");
// print_r($current_blog_id);
//获取主站十条内容,以分配到子站
$args = array(
'posts_per_page' => 10, //需要提取的文章数
'post_type' => 'post',
'orderby'=>'date',
'order'=>'DESC',
//'category__in' => array( 16 ), // 获取ID为16的栏目的所有文章
'post_status' => 'draft', // 文章状态
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'artme', // 自定义字段名称
'compare' => 'NOT EXISTS' // 筛选出该自定义字段不存在的文章
)
)
);
$posts_ten_art = new WP_Query($args);
$posts_ten = $posts_ten_art->posts;
foreach ($posts_ten as $t){
$t->meta = get_post_meta($t->ID,'artme',true);
}
//获取所有子站列表
$blogList = get_blog_list(0, 'all');
print_r("<pre>");
print_r($blogList);
if (count($posts_ten) > 0) {
foreach ($posts_ten as $val){
//随机获取子站
$mt_rands = mt_rand(0,count($blogList)-1);
//随机分配子站BLOG_ID
$ablogids_is = $blogList[$mt_rands]['blog_id'];
print_r("<pre>");
print_r($mt_rands . '----' . $ablogids_is);
// 切换到子站点
switch_to_blog($ablogids_is);
print_r("<pre>");
print_r(get_current_blog_id());
//找出所有栏目
$cate = get_terms(array(
'taxonomy' => 'category',
'hide_empty' => false,
));
$mt_cate_rands = mt_rand(0,(count($cate)-2));
$mt_cate_rands_id = $cate[$mt_cate_rands]->term_id;
$post_arr = array(
'post_author' => 1,
'post_title' => $val->post_title,
'post_content' => $val->post_content,
'post_status' => 'publish',
'post_type' => 'post',
'post_category' => array($mt_cate_rands_id)
);
$result = wp_insert_post($post_arr);
print_r("<pre>");
print_r($result . 'qqqqqqqqqqq');
// 恢复当前站点
//switch_to_blog(1);
restore_current_blog();
// print_r("<pre>");
// print_r(get_current_blog_id());
if($result){
add_post_meta($val->ID, 'artme', '2');
print_r(get_post_meta($val->ID, 'artme',true) . 'ppppppppppp');
$log_message = date('Y-m-d H:i:s') . '----' . $blogList[$mt_rands]['domain'] . '----------' . $val->post_title . "\n";
// 写入日志文件
file_put_contents($log_file, $log_message, FILE_APPEND);
echo "新增数据" . PHP_EOL;
}
}
}else{
$log_message = date('Y-m-d H:i:s'). "暂无数据!" . "\n";
// 写入日志文件
file_put_contents($log_file, $log_message, FILE_APPEND);
echo "新增数据" . PHP_EOL;
}
?>
<?php
require_once( dirname( __FILE__ ) . '/wp-load.php' );
global $wpdb;
$log_file = 'log/sql.log'; // 日志文件路径
// 主数据库链接参数
$host = DB_HOST;
$username = DB_USER;
$password = DB_PASSWORD;
$database = DB_NAME;
// 链接数据库
$wpdb = new wpdb($username, $password, $database, $host);
$results = $wpdb->get_results("SELECT ID,post_title,post_name,post_content FROM `wp_posts` where post_title <> '' and ping_status = 'open' and post_status = 'publish' and publish_status = 1 order by ID asc limit 0,10");
if(empty($results)){
// 格式化日志内容
$log_message = date('Y-m-d H:i:s') . " - 数据为空 \n";
// 写入日志文件
file_put_contents($log_file, $log_message, FILE_APPEND);
echo "数据为空".PHP_EOL;
exit;
}
// 随机数字 wp_posts 43~
$rand = mt_rand(43,87);
// $rand = 36;
foreach ($results as $result) {
// 处理查询结果
try {
// 表名
$table_name = "wp_{$rand}_posts";
// 分类表名
$category_name = "wp_{$rand}_term_relationships";
// 分类的随机id 1-13
$count = $wpdb->get_var($wpdb->prepare( "select count(*) as coun from wp_{$rand}_term_taxonomy where taxonomy = 'category' " ));
// echo $rand;
// echo ($count);exit;
$category_rand = mt_rand(1,$count);
// 格式化日志内容
$log_message = date('Y-m-d H:i:s') . " - " . $table_name . "\n";
// 写入日志文件
file_put_contents($log_file, $log_message, FILE_APPEND);
// 执行插入操作
$insertResult = $wpdb->query(
$wpdb->prepare(
"INSERT INTO $table_name (post_author, post_title, post_name, post_content, post_status, post_date, post_date_gmt, post_modified, post_modified_gmt) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
1,
$result->post_title,
$result->post_name,
$result->post_content,
'publish',
date('Y-m-d H:i:s'),
date('Y-m-d H:i:s'),
date('Y-m-d H:i:s'),
date('Y-m-d H:i:s')
)
);
$logData = array(
'title' => "新增数据 标题为:{$result->post_title}",
'rand' => $rand,
'data' => $data
);
// 格式化日志内容
$log_message = date('Y-m-d H:i:s') . " - " . json_encode($logData,JSON_UNESCAPED_UNICODE) . "\n";
// 写入日志文件
file_put_contents($log_file, $log_message, FILE_APPEND);
echo "新增数据{$rand}".PHP_EOL;
if($insertResult){
if ( $insertResult ) {
$lastInsertId = $wpdb->insert_id;
echo "刚刚插入的记录的 ID 是:$lastInsertId".PHP_EOL;
$wpdb->query(
$wpdb->prepare(
"INSERT INTO $category_name (object_id, term_taxonomy_id) VALUES (%s, %s)",
$lastInsertId,
$category_rand
)
);
} else {
echo "插入操作失败".PHP_EOL;
}
// 新增成功之后,更新查询的数据状态
$saveData = array(
'publish_status' => 1
);
$updateResult = $wpdb->query(
$wpdb->prepare(
"update wp_posts set publish_status = 2 where ID = {$result->ID} "
)
);
$logData = array(
'title' => "更新查询的数据状态 标题为:{$result->post_title}",
'ID' => $result->ID,
'data' => $saveData
);
// 格式化日志内容
$log_message = date('Y-m-d H:i:s') . " - " . json_encode($logData,JSON_UNESCAPED_UNICODE) . "\n";
// 写入日志文件
file_put_contents($log_file, $log_message, FILE_APPEND);
echo "更新查询的数据状态".PHP_EOL;
}
} catch (Exception $e) {
echo '出现异常'.$e->getMessage().PHP_EOL;
$data = '出现异常'.$e->getMessage();
// 格式化日志内容
$log_message = date('Y-m-d H:i:s') . " - " . $data . "\n";
// 写入日志文件
file_put_contents($log_file, $log_message, FILE_APPEND);
}
}