我的WP媒体库中有数百幅艺术图像,每幅图像我都想转换为woocommerce产品,并设置为该产品的特色图像。
有没有办法在MySQL中自动添加新产品和设置特色图片,而不是手动添加新产品和设置特色图片,这需要几天的重复工作?
我的WP媒体库中有数百幅艺术图像,每幅图像我都想转换为woocommerce产品,并设置为该产品的特色图像。
有没有办法在MySQL中自动添加新产品和设置特色图片,而不是手动添加新产品和设置特色图片,这需要几天的重复工作?
我同意,在SQL中这样做比在PHP中学习要多得多。使用WordPress功能,您可以对所有图像运行查询(WP_Query
) 然后循环浏览结果,并使用图像中的信息创建新帖子,使用[wp_insert_post()][2]
. 最后你可以更新那篇文章的_thumbnail_id
元密钥([update_post_meta()][3]
, 存储特征图像的ID。
完全未经测试:
function wpa_convert_images_to_products(){
$args = array(
\'post_type\' => \'attachment\', // Only bring back attachments
\'post_mime_type\' => \'image\', // Only bring back attachments that are images
\'posts_per_page\' => \'-1\', // get them all
\'post_status\' => \'inherit\', // Attachments default to "inherit", rather than published. Use "inherit" or "all".
);
$images = new WP_Query( $args );
if ( $images->have_posts() ) while( $images->have_posts() ) {
$images->the_post();
$post = array(
\'post_title\' => get_the_title(),
\'post_excerpt\' => get_the_excerpt(),
\'post_type\' => \'product\',
);
$post_id = wp_insert_post( $post );
update_post_meta( $post_id, \'_thumbnail_id\', get_the_ID() );
}
}
add_action(\'admin_init\', \'wpa_convert_images_to_products\' );
你只想运行一次,否则你会得到一小吨新产品。Editing to add 您可以确保使用瞬态只运行一次。
function wpa_convert_images_to_products(){
if ( false === get_transient( \'wpa_convert_images_to_products\' ) ) ) {
$args = array(
\'post_type\' => \'attachment\', // Only bring back attachments
\'post_mime_type\' => \'image\', // Only bring back attachments that are images
\'posts_per_page\' => \'-1\', // get them all
\'post_status\' => \'inherit\', // Attachments default to "inherit", rather than published. Use "inherit" or "all".
);
$images = new WP_Query( $args );
if ( $images->have_posts() ) {
while( $images->have_posts() ) {
$images->the_post();
$post = array(
\'post_title\' => get_the_title(),
\'post_excerpt\' => get_the_excerpt(),
\'post_type\' => \'product\',
);
$post_id = wp_insert_post( $post );
update_post_meta( $post_id, \'_thumbnail_id\', get_the_ID() );
}
set_transient( \'wpa_convert_images_to_products\', $images );
}
}
add_action( \'admin_init\', \'wpa_convert_images_to_products\' );
您想要创建Woocommerce产品,并且要做好这项工作,您不仅需要post对象和缩略图:您还需要分类法和自定义字段:我认为您的产品至少会有一个价格,不是吗?
一旦您想要批量创建产品,我想对所有产品使用相同的分类法和相同的自定义字段对您来说都是有益的(无论如何,手动更改总是可能的)。
我将提出一种方法,并给出相关代码,但只需注意not 已测试。
First of all, manually create one product: 这将是你的reference 产品
为它设定一切:价格、变化、分类。。。每件事同时设置缩略图,偏离路线。
现在只是take note of the post ID 本产品的。
第二步是创建一个函数,该函数可以循环浏览附件并创建帖子、分配缩略图、自定义字段和分类法。
建议:请注意,woocommerce使用一些自定义表来保存一些信息,我的方法会忽略这些表,因此您不会将有关可下载产品的信息/设置批量分配给帖子。如果您想从图像中创建可下载的产品,则必须在使用该功能之前对其进行修改。此外,一些WooCommerce插件也使用自定义表:在运行函数之前请注意这一点
function wpa_convert_images_to_products($ref_id = 0, $skip_images = array() ) {
// following line ensure that present function is runned once
if ( get_transient(\'convert_images_to_products_done\') ) return;
//try to remove php limits in execution time an memory
@set_time_limit (0);
@ini_set(\'memory_limit\', -1);
if ( ! post_type_exists( \'product\' ) || ! $ref_id )
wp_die(\'Reference post id is not valid or product post type is not registered.\');
$reference = get_post($ref_id);
if ( ! $reference )
wp_die(\'Given reference post id is not valid.\');
$ref_thumb = get_post_thumbnail_id( $ref_id );
if ( ! is_int($ref_thumb) ) $ref_thumb = null;
// get reference attributes
$product_vars = get_object_vars($reference);
unset($product_vars[\'ID\']);
unset($product_vars[\'post_date\']);
unset($product_vars[\'post_date_gmt\']);
unset($product_vars[\'post_modified\']);
unset($product_vars[\'post_modified_gmt\']);
unset($product_vars[\'comment_count\']);
// get reference custom fields
$ref_fields = get_post_custom( $ref_id );
// get reference taxonomies
$all_tax = get_object_taxonomies(\'product\');
if ( ! empty($all_tax) ) {
$ref_tax = wp_get_object_terms( $ref_id, $all_tax, array(\'fields\' => \'all\') );
}
// skip reference thumbnail and images passed as second param in function
$skip_images = array_merge( (array)$skip_images, array($ref_thumb) );
$args = array(
\'post__not_in\' => $skip_images,
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'posts_per_page\' => \'-1\',
\'post_status\' => \'inherit\',
);
$images = new WP_Query( $args );
$errors = array();
// start loop through images
if ( $images->have_posts() ) :
// after that any other function call will fail
set_transient( \'convert_images_to_products_done\', 1);
global $wpdb;
while( $images->have_posts() ) :
$images->the_post();
global $post;
$image = $post->ID;
$excerpt = get_the_excerpt();
if ( empty($excerpt) ) $excerpt = get_the_title();
$product_vars[\'post_title\'] = get_the_title();
$product_vars[\'post_excerpt\'] = $excerpt;
$product = wp_insert_post( $product_vars );
if ( intval($product) ) {
// insert custom fields
if ( ! empty($ref_fields) ) {
$meta_insert_query = "INSERT INTO $wpdb->postmeta \';
$meta_insert_query .= \'(meta_key, meta_value) VALUES ";
$values = \'\';
foreach ( $ref_fields as $key => $array ) {
if ( $key != \'_thumbnail_id\' ) {
foreach ( $array as $value ) {
if ( $values != \'\' ) $values .= \', \';
$values .= $wpdb->prepare( \'(%s, %s)\', $key, maybe_serialize($value) );
}
}
}
if ( $values != \'\' ) {
$meta_insert_query .= $values;
if ( ! $wpdb->query( $meta_insert_query ) ) {
$error = \'Fail on inserting meta query for product \';
$error .= $product . \'. Query: \' . $meta_insert_query;
$errors[] = $error;
}
}
}
// insert taxonomies
if ( ! empty($ref_tax) && ! is_wp_error( $ref_tax ) ) {
$taxonomies = array();
foreach ( $ref_tax as $term ) {
if ( ! isset($taxonomies[$term->taxonomy]) )
$taxonomies[$term->taxonomy] = array();
$taxonomies[$term->taxonomy][] = $term->slug;
}
foreach ( $taxonomies as $tax => $terms ) {
$set_tax = wp_set_post_terms( $product, $terms, $tax, false );
if ( ! is_array($set_tax) ) {
$error = \'Fail on insert terms of taxonomy \';
$error .= $tax . \' for product\' . $product;
if ( is_string( $set_tax ) )
$error .= \' First offending term \' . $set_tax;
if ( is_wp_error($set_tax) )
$error .= \' Error: \' . $set_tax->get_error_message();
$errors[] = $error;
}
}
}
if ( ! set_post_thumbnail( $product, $image ) ) {
$error = \'Set thumbnail failed for product \';
$error .= $product . \' image \' . $image;
$errors[] = $error;
}
} else {
$errors[] = \'Insert post failed for image with id \' . $image;
}
endwhile;
else :
wp_die(\'You have no media.\');
endif;
wp_reset_postdata();
if ( ! empty($errors) )
wp_die(\'<p>\' . implode(\'</p><p>\', $errors) . \'</p>\');
}
。。。是的,这是一个怪物功能。。。第三步也是最后一步是最简单的,您必须调用刚刚创建的函数,并将引用产品id作为param传递。完成此任务的方法有很多,这里只有一种:
function launch_convert_images_to_products() {
//
// REPLACE THE 0 IN FUNCTION PARAM BELOW WITH YOUR REFERENCE PRODUCT ID
//
wpa_convert_images_to_products( 0 );
//
//
}
add_action(\'admin_init\', \'launch_convert_images_to_products\', 30);
请注意,通常该功能将为创建产品all 您的图像。我将第二个参数添加到wpa_convert_images_to_products
可以是要跳过的图像id数组的函数。有选择地从图像创建产品的另一种方法是为附件(信息here) 然后使用适当的tax_query
在图像WP\\U查询中,选择要转换的图像。
希望有帮助。
不可以。MySQL中没有可以将条目转换为woocommerce产品特定格式的命令。原因是数据库不“知道”这种格式。
有一种命令语言可以做到这一点。您可以使用SQL 查询数据库以直接更改数据库(不使用PHP)。
有时直接查询数据库以处理大量数据更容易。这是首先使用数据库的一个原因。最终,这就是Woo Commerce在PHP中所做的。
考虑到您正在问这个问题,您不太可能已经拥有编写这个查询的知识,而没有(可能是陡峭的)学习曲线。如果手工转换产品确实需要很长时间,请聘请熟悉格式的专业人员为您转换数据。你节省的时间可能值得付出代价。联系Woo Commerce的作者可能是找到那个人的好资源。
如果您有一些时间,并且您想自己做这件事,那么首先要做的是找出Woo Commerce的产品存储格式。
INSERT INTO wp_hgwrxz_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 (
\'1\',
current_date(),
current_date(),
\'HTML\',
\'Custom Printed Tape\',
\'\',
\'publish\',
\'open\',
\'closed\',
\'\',
\'custom-printed-tape\',
\'\',
\'\',
current_time(),
current_time(),
\'\',
\'0\',
\'http://zastil.co.uk/?post_type=product&p=14\',
\'0\',
\'product\',
\'\',
\'0\'
)
是否存在一些MYSQL查询来查找和替换wp\\U帖子,并忽略所有其他wordpress和buddypress帖子(buddypress文档)以及自定义帖子类型=公文包?我只想查找和替换wordpress博客内容,不想接触其他帖子。我使用以下MYSQL查询:UPDATE wp_posts SET post_content = REPLACE (post_content,\'Item to replace here\',\'Replacement text here\');