wordpress - wp_insert_post 生成重复 id
问题描述
我使用以下代码为产品变体生成 id。
$i = 0;
while($i < 2){ // 2 is example
$product = wc_get_product($product_id);
$variation_post = array(
'post_title' => 'Variation #' . $i . ' of ' . $product->get_name(),
'post_name' => 'product-'.$product_id.'-variation',
'post_status' => 'publish',
'post_parent' => $product_id,
'post_type' => 'product_variation',
'guid' => $product->get_permalink()
);
if((post_exists('Variation #' . $i . ' of ' . $product->get_name())) == 0){
$variation_id = wp_insert_post( $variation_post );
}
$variation = new WC_Product_Variation( $variation_id );
$i++;
}
但wp_insert_post
生成两个 id 而不是一个。我在ajax文件中使用这段代码,问题出在哪里?!
解决方案
推荐阅读
- android - 如何通过意图共享任何类型的文件(.pdf、.xls、.docs)?
- angular - 如何更改 Angular Material 步进器中的状态图标?
- mysql - 如何从另一个服务器存储过程调用存储过程?
- docker - 使用 Docker 运行时如何备份和恢复 Alfresco Community
- amazon-ec2 - 与 EC2 上托管的 websocket 服务器的 ws 连接上的 Lambda 超时错误
- selenium - 当服务器使用 w3c 时,在 Edge 中获取错误消息“不允许使用 JSON 有线协议命令端点”
- kendo-grid - 导出到 Excel 按钮在 Edge 浏览器中不起作用
- azure - Azure 数据工厂:从 postgresql 数据库复制数据(或数据流)
- python - 验证字符串是否是python中的有效HTML?
- java - Java中有没有办法使用json文件中的jsonpointer获取行号