添加新产品以更新产品,然后在永久链接中设置帖子标题之前设置sku编号永久链接。产品导入然后在更新产品之前在永久链接中设置sku编号。
我尝试了以下代码:
add_action('save_post', 'change_title');
function change_title($post_id) {
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'change_title');
$_sku = get_post_meta( $post_id, '_sku', true);
$args = array('ID'=>$post_id,'post_name'=>$_sku,);
// update the post, which calls save_post again
wp_update_post( $args );
// re-hook this function
add_action('save_post', 'change_title');
}
答案 0 :(得分:0)
试试这个
function update_product_link( $post_id, $post, $update ) {
if( $post->post_type != "product" ) {
return;
}
$_sku = get_post_meta( $post_id, '_sku', true);
//This will update post name to SKU
$post->post_name = $_sku;
}
add_action( 'save_post', 'update_product_link', 1, 3 );
还要将产品永久链接基础结构设置为/product/%postname%/
答案 1 :(得分:-1)
尝试:
function change_title( $post_id ){
$_sku = get_post_meta( $post_id, '_sku', true);
global $wpdb;
$wpdb->update( $wpdb->posts, array( 'post_title' => $_sku ), array( 'ID' => $post_id ) );
}
add_action( 'save_post', 'change_title');