A
A
Artem Kolchin2022-04-19 13:51:09
WordPress
Artem Kolchin, 2022-04-19 13:51:09

What does this function do and how to add a condition?

Learn the basics of wordpress

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


As I understand it, she watches post status change events and is already clearing the cache.
A question of such a plan, and if I have an event post type, is
it correct to write
add_action( 'transition_post_status', function ( $new_status, $old_status, $post ) {
      if ( "post" == $post->post_type && 'publish' === $new_status || "event" == $post->post_type && 'publish' === $new_status ) {
        delete_transient( self::$cache_filed );
      }
    }, 10, 3 );

Answer the question

In order to leave comments, you need to log in

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

You have interesting basics) You can shorten it a little by checking the current post_type in an array

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 );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question