我正在使用transition_post_status
防止基于帖子元字段发布自定义帖子类型。这似乎没有完全奏效,这就是我需要一些帮助的地方。
以下是我所拥有内容的简化版本:
add_action( \'transition_post_status\', [ $this, \'intercept_adherence_publishing\' ], 9, 3 );
function intercept_adherence_publishing( $new_status, $old_status, $post ) {
$post_id = $post->ID;
$adherence_status = $_POST[\'_adherence_status\'];
if ( ( $new_status === \'publish\' ) && ( $post->post_type == \'protocol-adherence\' ) && ( $adherence_status !== \'accepted\' ) ) {
error_out(\'Published post \' . $post_id . \' intercepted. Post remains unpublished due to adherence not being accepted\');
wp_die( \'<b>Adherence Error: </b>Cannot publish adherence that is not accepted. Please save status in "Pending Review" instead of the publish button if the adherence is not accepted yet.\', \'Adherence Publishing Error\', [ \'back_link\' => true ]);
}
}
What is supposed to happen when post meta is not "accepted":
最合适的回答,由SO网友:mrben522 整理而成
我认为hook会在post状态更新后触发。试试这个
add_action( \'pre_post_update\', \'intercept_adherence_publishing\', 10, 2);
function intercept_adherence_publishing ($post_ID, $data ) {
if (get_post_type($post_ID) !== \'protocol-adherence\') {
return;
}
$post = get_post($post_ID);
$adherence_status = $_POST[\'_adherence_status\'];
if ( ( $data[\'post_status\'] === \'publish\' ) && ( $post->post_type == \'protocol-adherence\' ) && ( $adherence_status !== \'accepted\' ) ) {
error_out(\'Published post \' . $post_ID . \' intercepted. Post remains unpublished due to adherence not being accepted\');
wp_die( \'<b>Adherence Error: </b>Cannot publish adherence that is not accepted. Please save status in "Pending Review" instead of the publish button if the adherence is not accepted yet.\', \'Adherence Publishing Error\', [ \'back_link\' => true ]);
}
}