这个publish_{post-type}
只有当帖子状态(未发布)变为已发布时,才会触发动作;例如,如果帖子已经发布,并且您对其进行了编辑publish_{post-type}
未触发操作。我想你需要把你的函数save_post_{post_type}
无论状态如何,每次保存帖子时都会触发操作:
add_action(\'save_post_course-variation\', \'update_course_variation_permalink\', 10, 3);
function update_course_variation_permalink($course_variation_id){
$cv_meta = get_post_meta($course_variation_id);
$desired_permalink_value = "my/custom/permalink";
if( isset($cv_meta[\'custom_permalink\']) ){
//HAS PERMALINK = CHECK AND UPDATE IF REQUIRED
if( get_post_meta($course_variation_id, \'custom_permalink\', true) != $desired_permalink_value ){
update_post_meta($course_variation_id, \'custom_permalink\', $desired_permalink_value);
$results = "UPDATED";
}else{
$results = "UNTOUCHED/ALREADY SET CORRECTLY";
}
}else{
//DOESNT HAVE PERMALINK = ADD
add_post_meta($course_variation_id, \'custom_permalink\', $desired_permalink_value, true);
$results = "NEW CUSTOM PERMALINK CREATED";
}
}//end function
Note: 将数据存储在
custom_permalink
元字段对永久链接没有影响。不确定您是如何处理post permalink的。我刚刚回答了您关于更新帖子时如何触发该功能的问题。如果您想真正重写permalink(不是元字段),可以使用它(基于此
answer):
add_filter(\'wp_insert_post_data\', \'wpse_wp_insert_post_data\', 10, 2);
function wpse_wp_insert_post_data($data, $post_attr) {
$desired_permalink_value = "my/custom/permalink";
if( ( isset( $data[\'post_name\'] ) && $data[\'post_name\'] != $desired_permalink_value ) || empty( $data[\'post_name\'] ) ) {
$data[\'post_name\'] = $desired_permalink_value;
}
return $data;
}