There are three hooks specifically for when the status of a post changes:
/**
* Fires when a post is transitioned from one status to another.
*/
do_action( 'transition_post_status', $new_status, $old_status, $post );
/**
* Fires when a post is transitioned from one status to another.
*
* The dynamic portions of the hook name, `$new_status` and `$old_status`,
* refer to the old and new post statuses, respectively.
*/
do_action( "{$old_status}_to_{$new_status}", $post );
/**
* Fires when a post is transitioned from one status to another.
*
* The dynamic portions of the hook name, `$new_status` and `$post->post_type`,
* refer to the new post status and post type, respectively.
*/
do_action( "{$new_status}_{$post->post_type}", $post->ID, $post );
How to use:
/**
* Portfolio always published as private
*
* @param $post_id
*/
function wp_neavr2_portfolio_always_publish_as_private( $post_id ) {
wp_update_post( array(
'ID' => $post_id,
'post_status' => 'private',
) );
}
add_action( 'publish_portfolio', 'wp_neavr2_portfolio_always_publish_as_private' );
In this case, the portfolio
post type will always be published privately. However, it is possible to create a draft, as this is not restricted.
Note that since this is not a filter, you must use the wp_update_post()
function to update the status.