A
A
Artem Kolchin2022-04-19 15:58:08
WordPress
Artem Kolchin, 2022-04-19 15:58:08

How to run transition_post_status?

Tell me, there is a transition_post_status function
, the execution of the function hangs on it.

add_action( 'transition_post_status', function ( $new_status, $old_status, $post ) {
  if ( in_array( $post->post_type, ['post', 'event'] ) && 'publish' === $new_status ) {
    delete_transient( self::$cache_filed );
  }
}, 10, 3 );


in the previous question, I was told how to check in the array for the post type, but now the question is of such a plan.
how to execute transition_post_status if the post is already published? If you update, does post_update work?
but I need exactly transition_post_status, how to run it for a specific article, for example.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Zolin, 2022-04-19
@artzolin

For the record save event, you can use a hook save_post, example:

add_action( 'save_post', 'custom_save_post' );
function custom_save_post( $post_id ) {

  // добавляем стартовое кол-во просмотров для всех публикуемых страниц
  add_post_meta( $post_id, 'views', random_int(20, 50), true );
  
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question