Mini Shell
<?php
$conn = new mysqli('localhost', 'duytan', 'tandb', 'thienwp'); //db site cũ
$conn2 = new mysqli('localhost', 'duytan', 'tandb', 'thienwp'); //db site mới
$sql_wp_posts = "INSERT INTO wp_posts (post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_content_filtered, post_parent, guid, menu_order, post_type, post_mime_type, comment_count) VALUES ";
$sql_wp_postmeta = "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES ";
$FIELDS = ['asin', 'single_image_old', 'external_id', 'source' ];
echo count($FIELDS);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
echo "Loi ket noi co sơ du lieu xem lai cau hinh";
}
else
{
$sql = "SELECT * FROM wp_posts WHERE post_status = 'publish' AND post_type='product' and post_parent=0 LIMIT 1"; //ORDER BY id DESC
$result = $conn->query($sql) or die($conn->error);
if ($result->num_rows > 0)
{
echo "Đã kết nối" . PHP_EOL;
$stt = 1;
while ($row = $result->fetch_assoc())
{
//$sqlmeta = "SELECT * FROM wp_postmeta WHERE post_id=".$row['ID']." AND meta_key = 'product_json' ";
//$result_meta = mysqli_query($conn, $sqlmeta);
//$rowmeta = mysqli_fetch_row($result_meta);
$datenow = date('Y-m-d H:i:s');
$post_author = '1';
$post_date = "$datenow";
$post_date_gmt = "$datenow";
$post_content = mysqli_real_escape_string($conn, ($row['post_content']));
$post_title = mysqli_real_escape_string($conn, ($row['post_title']));
$post_excerpt = '';
$post_status = 'publish';
$comment_status = 'open';
$ping_status = 'closed';
$post_password = '';
$post_name = $row['post_name'].'-'.rand(222, 444); //handle;
$to_ping = '';
$pinged = '';
$post_modified = $datenow;
$post_modified_gmt = $datenow;
$post_content_filtered = '';
$post_parent = 0;
$guid = $row['guid']; //url
$menu_order = '0';
$post_type = 'product';
$post_mime_type = '';
$comment_count = '0';
$_stringQueryPosts = $sql_wp_posts . "('$post_author', '$post_date', '$post_date_gmt', '$post_content', '$post_title', '$post_excerpt', '$post_status', '$comment_status', '$ping_status', '$post_password', '$post_name', '$to_ping', '$pinged', '$post_modified', '$post_modified_gmt', '$post_content_filtered', '$post_parent', '$guid', '$menu_order', '$post_type', '$post_mime_type', '$comment_count')";
//echo $_stringQueryPosts;
if (mysqli_query($conn2, $_stringQueryPosts))
{
$last_id = $conn2->insert_id;
echo "Đã thêm thành công product: " . $last_id . PHP_EOL;
$post_id = "$last_id";
$meta_key = '_price';
$meta_value = '17.95';
//gan post type cho product
//add wp_term_relationships oject_id = post_id, term_taxonomy_id = 795 (mmolazy gearment)
$set_term = "INSERT INTO wp_term_relationships (object_id, term_taxonomy_id, term_order) VALUES ($post_id, '795', '0')";
mysqli_query($conn2, $set_term);
$_stringQueryPostMeta = $sql_wp_postmeta . "('$post_id', '$meta_key', '$meta_value')";
$post_id2 = "$last_id";
foreach ($FIELDS as $key => $field)
{
$meta_key2 = $field;
$meta_value_field = check_field($row['ID'], $field, $conn);
if ($meta_value_field)
{
$meta_value2 = mysqli_real_escape_string($conn, $meta_value_field);
// var_dump($meta_value2 );
// exit();
$_stringQueryPostMeta2 = $sql_wp_postmeta . "('$post_id2', '$meta_key2', '$meta_value2')";
//echo "\n";
//echo $_stringQueryPostMeta2;
//echo "\n";
if (mysqli_query($conn2, $_stringQueryPostMeta) and mysqli_query($conn2, $_stringQueryPostMeta2))
{
//echo "$stt Đã thêm thành công wp_post_meta cho id product: " . $last_id . PHP_EOL;
$stt++;
}
else
{
//echo $_stringQueryPostMeta2 . PHP_EOL;
echo "Loi ko them dc metapost: " . "<br>" . $conn2->error . PHP_EOL;
}
}
// echo "key=";
// echo $key;
// echo "\n";
}
}
else
{
echo "Loi ko them dc wp_posts: " . "<br>" . $conn2->error . PHP_EOL;
}
} // end while
}
}
$conn->close();
function check_field($post_id, $meta_key, $conn)
{
$sqlmeta = "SELECT * FROM wp_postmeta WHERE post_id=" . $post_id . " AND meta_key = '" . $meta_key . "' ";
//echo $sqlmeta;
//echo "\n";
$result_meta = mysqli_query($conn, $sqlmeta);
$result = false;
if (mysqli_num_rows($result_meta) == 0)
{
$result = false;
}
else
{
$rowmeta = mysqli_fetch_row($result_meta);
$result = $rowmeta[3];
}
return $result;
}