所以我使用woocommerce,并使用一个名为WP-All-Import的插件将产品数据库导入我的Quickbooks连接的电子商务平台。现在我需要更改元"_sync_status"为"on"为所有产品完成后。我该如何对所有添加的产品进行处理呢?
首先需要获取所有WooCommerce Products (post type of "product"),遍历每个产品,并更新每个产品的post meta。你可以把它放在主题的functions.php
, /wp-content/mu-plugins
"必须使用"插件目录下的一个文件,或者匿名使用WordPress Developer + Console之类的插件来运行这段代码。
// args to fetch all products
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
// create a custom query
$products = new WP_Query( $args );
// if products were returned...
if ( $products->have_posts() ):
// loop over them....
while ( $products->have_posts() ):
// using the_post() to set up the $post
$products->the_post();
// use $post->ID to update the '_sync_status' post meta value
update_post_meta( $post->ID, '_sync_status', 'on' );
endwhile;
endif;
谢谢你为我所做的一切我可以更新和添加新的post meta到我的产品。
// args to fetch all products
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
// create a custom query
$products = new WP_Query( $args );
// if products were returned...
if ( $products->have_posts() ):
// loop over them....
while ( $products->have_posts() ):
$products->the_post();
if (get_post_meta(get_the_ID(),'slide_template')=="" || !get_post_meta(get_the_ID(),'slide_template')){
update_post_meta( get_the_ID(), 'slide_template', 'default' );
echo get_the_ID().' do it'.'<br>';
}
endwhile;
endif;
pre_post_update
钩子在保存文章之前被调用。
如果您希望在它们被创建或更新时这样做,您可以劫持REQUEST数组
add_action('pre_post_update', 'before_data_is_saved_function');
function before_data_is_saved_function($post_id) {
if ($_REQUEST['post_type'] == 'product') {
$_REQUEST['_sync_status'] = 'on';
}
}
如果你只想在它们"被添加"时执行此操作,请使用:
add_action('pre_post_update', 'before_data_is_saved_function');
function before_data_is_saved_function($post_id) {
if ($_REQUEST['post_type'] == 'product' && $_REQUEST['publish'] = 'Publish') {
$_REQUEST['_sync_status'] = 'on';
}
}