钩子文档

after_delete_post

💡 云策文档标注

概述

after_delete_post 是一个 WordPress 动作钩子,在 wp_delete_post() 函数执行完毕后触发,用于在文章删除后执行自定义代码。它提供文章 ID 和文章对象作为参数,支持基于条件(如文章类型、状态或自定义字段)执行特定操作。

关键要点

  • 触发时机:在 wp_delete_post() 函数结束时触发,文章已从数据库中删除。
  • 参数:$post_id(整数,文章 ID)和 $post(WP_Post 对象,文章对象)。
  • 用途:常用于清理相关数据、记录日志或执行其他后处理任务。
  • 版本历史:WordPress 5.5.0 添加了 $post 参数,3.2.0 引入此钩子。

代码示例

add_action( 'after_delete_post', 'aar_do_something', 10, 2 );
function aar_do_something( $post_id, $post ) {
    // 针对特定文章类型 'books' 执行操作
    if ( 'books' !== $post->post_type ) {
        return;
    }
    // 在此处编写自定义代码
}

注意事项

  • 钩子执行时文章已从数据库删除,因此无法再通过 get_post() 等函数获取文章数据,但 $post 参数提供了删除前的文章对象。
  • 建议在函数中检查条件(如文章类型、状态或自定义字段),以避免不必要的操作。
  • 代码应添加到主题的 functions.php 文件或自定义插件中,确保兼容性和可维护性。

📄 原文内容

Fires after a post is deleted, at the conclusion of wp_delete_post() .

Description

See also

Parameters

$post_idint
Post ID.
$postWP_Post
Post object.

Source

do_action( 'after_delete_post', $post_id, $post );

Changelog

Version Description
5.5.0 Added the $post parameter.
3.2.0 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    Performing Actions After Deleting a Post Based on Multiple Conditions

    In this tutorial, you will learn how to execute custom code after a WordPress post is deleted, but only under specific conditions. We will use a combination of post type, post status, and custom field value checks to determine whether to run the custom code.

    Step 1: Hook into the after_delete_post Action

    First, we need to hook into the after_delete_post action, which fires after a post is deleted. Add the following code to your theme’s functions.php file or your custom plugin file:

    add_action( 'after_delete_post', 'wpdocs_do_something', 10, 2 );

    Step 2: Define the wpdocs_do_something Function

    function wpdocs_do_something( $post_id, $post ) {
        // Condition 1: Check for specific post types
        $allowed_post_types = array( 'books', 'movies', 'products' );
        if ( ! in_array( $post->post_type, $allowed_post_types ) ) {
            return;
        }
    
        // Condition 2: Check for specific post statuses (e.g., only if the post was published)
        $allowed_post_statuses = array( 'publish', 'private' );
        if ( ! in_array( get_post_status( $post_id ), $allowed_post_statuses ) ) {
            return;
        }
    
        // Condition 3: Check for a specific custom field value
        $custom_field_value = get_post_meta( $post_id, 'wpdocs_field_key', true );
        if ( 'specific_value' !== $custom_field_value ) {
            return;
        }
    
        // Write your code here. All conditions met
        // Example: Log the deletion
        error_log( 'Post with ID ' . $post_id . ' and type ' . $post->post_type . ' has been deleted.' );
    
        // Add your custom code here
        // Example: Remove related data from a custom table
        global $wpdb;
        $wpdb->delete( $wpdb->prefix . 'custom_table', array( 'post_id' => $post_id ) );
    }