我有循环的数据,这些数据包括标题,内容,服务器上文件的路径以及各种其他信息.
目标是遍历数据并在循环中创建Worpress帖子,其中包含post_meta product_image的图片,如:add_post_meta($ post_id,'_ product_image',$ attach_id,true);
我的循环看起来像这样:
while($row = $STH->fetch()) {
$my_post = array(
'post_title' => $row->title,
'post_content' => $row->description,
'post_status' => 'publish',
'post_author' => $current_user->ID,
'post_category' => array(6)
);
// Insert the post into the database
$post_id = wp_insert_post( $my_post );
add_post_meta( $post_id, 'product_price', 199, true );
add_post_meta( $post_id, 'product_sub_price', 20, true);
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
$filename = $row->image_local_name;
// Path to the file i want to upload into wordpress.
$path = ABSPATH . …Run Code Online (Sandbox Code Playgroud)