钩wp_insert_post_data
筛选以强制将帖子状态设置为未完成,然后才能将其设置为已发布。使用以下代码,只能保存设置为未完成的帖子:
add_filter( \'wp_insert_post_data\', \'prevent_post_change\', 20, 2 );
function prevent_post_change( $data, $postarr ) {
if ( ! isset($postarr[\'ID\']) || ! $postarr[\'ID\'] ) return $data;
if ( $postarr[\'post_type\'] !== \'product\' ) return $data; // only for products
$old = get_post($postarr[\'ID\']); // the post before update
if (
$old->post_status !== \'incomplete\' &&
$old->post_status !== \'trash\' && // without this post restoring from trash fail
$data[\'post_status\'] === \'publish\'
) {
// set post to incomplete before being published
$data[\'post_status\'] = \'incomplete\';
}
return $data;
}