你所需要做的就是save_post
钩方法如下:
function change_post_status_based_on_custom_field( $post_id ) {
// If this is just a revision, don\'t do anything.
if ( wp_is_post_revision( $post_id ) )
return;
// Get field value
$value = get_post_meta( $post_id, \'played\', true );
$status = $value ? \'publish\' : \'draft\';
// If status should be different, change it
if ( get_post_status( $post_id ) != $status ) {
// unhook this function so it doesn\'t loop infinitely
remove_action( \'save_post\', \'change_post_status_based_on_custom_field\' );
// update the post, which calls save_post again
wp_update_post( array(
\'ID\' => $post_id,
\'post_status\' => $status
) );
// re-hook this function
add_action( \'save_post\', \'change_post_status_based_on_custom_field\' );
}
}
add_action( \'save_post\', \'change_post_status_based_on_custom_field\' );