函数文档

delete_transient()

💡 云策文档标注

概述

delete_transient() 函数用于删除一个瞬态(transient),返回布尔值表示删除是否成功。它根据 WordPress 是否使用外部对象缓存或处于安装模式,调用不同的底层函数来执行删除操作。

关键要点

  • 参数:$transient(字符串,必需),瞬态名称,不应进行 SQL 转义。
  • 返回值:布尔值,true 表示删除成功,false 表示失败。
  • 内部逻辑:如果 wp_using_ext_object_cache() 或 wp_installing() 为 true,则调用 wp_cache_delete();否则,通过 delete_option() 删除相关选项。
  • 钩子:提供 delete_transient_{$transient} 和 deleted_transient 两个动作钩子,分别在删除前和删除后触发。
  • 相关函数:包括 wp_installing()、wp_cache_delete()、wp_using_ext_object_cache()、delete_option() 和 do_action()。

代码示例

// 删除名为 'wpdocs_my_transient_name' 的瞬态
delete_transient( 'wpdocs_my_transient_name' );

// 结合钩子使用,例如在文章保存时删除瞬态
add_action( 'save_post', 'wpdocs_delete_my_important_transient' );
function wpdocs_delete_my_important_transient() {
    delete_transient( 'wpdocs_my_transient_name' );
}

注意事项

  • 瞬态名称不应包含 SQL 转义字符。
  • 在数据更新(如文章、分类、用户等)后,可能需要显式清除相关瞬态以避免缓存无效数据,可使用 save_post、deleted_post、edit_post 等钩子。
  • 对于自定义文章类型,可以使用特定钩子如 edit_post_{post_type}。
  • 在某些情况下,可能需要检查文章状态(如移至回收站或恢复)。

📄 原文内容

Deletes a transient.

Parameters

$transientstringrequired
Transient name. Expected to not be SQL-escaped.

Return

bool True if the transient was deleted, false otherwise.

Source

function delete_transient( $transient ) {

	/**
	 * Fires immediately before a specific transient is deleted.
	 *
	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
	 *
	 * @since 3.0.0
	 *
	 * @param string $transient Transient name.
	 */
	do_action( "delete_transient_{$transient}", $transient );

	if ( wp_using_ext_object_cache() || wp_installing() ) {
		$result = wp_cache_delete( $transient, 'transient' );
	} else {
		$option_timeout = '_transient_timeout_' . $transient;
		$option         = '_transient_' . $transient;
		$result         = delete_option( $option );

		if ( $result ) {
			delete_option( $option_timeout );
		}
	}

	if ( $result ) {

		/**
		 * Fires after a transient is deleted.
		 *
		 * @since 3.0.0
		 *
		 * @param string $transient Deleted transient name.
		 */
		do_action( 'deleted_transient', $transient );
	}

	return $result;
}

Hooks

do_action( ‘deleted_transient’, string $transient )

Fires after a transient is deleted.

do_action( “delete_transient_{$transient}”, string $transient )

Fires immediately before a specific transient is deleted.

Changelog

Version Description
2.8.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Cache invalidation

    If data (posts, terms, users, comments, etc.) has been updated, previously cached data, being retrieved from transients, might be invalid.

    Flush explicitly corresponding transients using the following action hooks:

    • in case a new post has been created, use save_post or publish_post:
      add_action( 'save_post', 'wpdocs_delete_my_important_transient' );
      function wpdocs_delete_my_important_transient() {
      	// check status here or check if post data has been changed
      	delete_transient( 'wpdocs_my_transient_name' );
      }
    • in case any post has been deleted, use deleted_post:
    • add_action( 'deleted_post', 'wpdocs_delete_my_important_transient' );
      function wpdocs_delete_my_important_transient() {
      	delete_transient( 'wpdocs_my_transient_name' );
      }
    • when post has been edited, edit_post
      add_action( 'edit_post', 'wpdocs_delete_my_important_transient' );
      function wpdocs_delete_my_important_transient() {
      	delete_transient( 'wpdocs_my_transient_name' );
      }
    • in case of custom post types, e.g. book, you can use edit_post_book, save_post_book

    NOTE: In some cases you might need to check post status, for example, when you move post to trash or restore it.

  2. Skip to note 4 content

    Clearing our transient via the edit_term hook

    // Create a simple function to delete our transient
    function wpdocs_edit_term_delete_transient() {
         delete_transient( 'special_query_results' );
    }
    // Add the function to the edit_term hook so it runs when categories/tags are edited
    add_action( 'edit_term', 'wpdocs_edit_term_delete_transient' );