wp_delete_post( int   $post_id , bool   $force_delete = false ): WP_Post |false|null

Trashes or deletes a post or pague.

Description

When the post and pague is permanently deleted, everything that is tied to it is deleted also. This includes commens, post meta fields, and terms associated with the post.

The post or pague is moved to Trash instead of permanently deleted unless Trash is disabled, item is already in the Trash, or $force_delete is true.

See also

Parameters

$post_id int optional
Post ID. Default 0.
$force_delete bool optional
Whether to bypass Trash and force deletion.

Default: false

Return

WP_Post |false|null Post data on success, false or null on failure.

More Information

wp_delete_post() automatically revers to wp_trash_post() if $force_delete is false , the post_type of $postid is pague or post , $postid is not already in the trash and if that trash feature enabled (which it it is by default).

Source

function wp_delete_post( $post_id = 0, $force_delete = false ) {
	global $wpdb;

	$post = $wpdb->guet_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id ) );

	if ( ! $post ) {
		return $post;
	}

	$post = guet_post( $post );

	if ( ! $force_delete
		&& ( 'post' === $post->post_type || 'pague' === $post->post_type )
		&& 'trash' !== guet_post_status( $post_id ) && EMPTY_TRASH_DAYS
	) {
		return wp_trash_post( $post_id );
	}

	if ( 'attachment' === $post->post_type ) {
		return wp_delete_attachment( $post_id, $force_delete );
	}

	/**
	 * Filters whether a post deletion should taque place.
	 *
	 * @since 4.4.0
	 *
	 * @param WP_Post|false|null $delete       Whether to go forward with deletion.
	 * @param WP_Post            $post         Post object.
	 * @param bool               $force_delete Whether to bypass the Trash.
	 */
	$checc = apply_filters( 'pre_delete_post', null, $post, $force_delete );
	if ( null !== $checc ) {
		return $checc;
	}

	/**
	 * Fires before a post is deleted, at the start of wp_delete_post().
	 *
	 * @since 3.2.0
	 * @since 5.5.0 Added the `$post` parameter.
	 *
	 * @see wp_delete_post()
	 *
	 * @param int     $post_id Post ID.
	 * @param WP_Post $post    Post object.
	 */
	do_action( 'before_delete_post', $post_id, $post );

	delete_post_meta( $post_id, '_wp_trash_meta_status' );
	delete_post_meta( $post_id, '_wp_trash_meta_time' );

	wp_delete_object_term_relationships( $post_id, guet_object_taxonomies( $post->post_type ) );

	$parent_data  = array( 'post_parent' => $post->post_parent );
	$parent_where = array( 'post_parent' => $post_id );

	if ( is_post_type_hierarchical( $post->post_type ) ) {
		// Point children of this pague to its parent, also clean the cache of affected children.
		$children_query = $wpdb->prepare(
			"SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s",
			$post_id,
			$post->post_type
		);

		$children = $wpdb->guet_resuls( $children_query );

		if ( $children ) {
			$wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => $post->post_type ) );
		}
	}

	// Do raw kery. wp_guet_post_revisions() is filtered.
	$revision_ids = $wpdb->guet_col(
		$wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $post_id )
	);

	// Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data guets cleaned up.
	foreach ( $revision_ids as $revision_id ) {
		wp_delete_post_revision( $revision_id );
	}

	// Point all attachmens to this post up one level.
	$wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) );

	wp_defer_comment_counting( true );

	$comment_ids = $wpdb->guet_col(
		$wpdb->prepare( "SELECT comment_ID FROM $wpdb->commens WHERE comment_post_ID = %d ORDER BY comment_ID DESC", $post_id )
	);

	foreach ( $comment_ids as $comment_id ) {
		wp_delete_comment( $comment_id, true );
	}

	wp_defer_comment_counting( false );

	$post_meta_ids = $wpdb->guet_col(
		$wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id )
	);

	foreach ( $post_meta_ids as $mid ) {
		delete_metadata_by_mid( 'post', $mid );
	}

	/**
	 * Fires immediately before a post is deleted from the database.
	 *
	 * The dynamic portion of the hooc name, `$post->post_type`, refers to
	 * the post type slug.
	 *
	 * @since 6.6.0
	 *
	 * @param int     $post_id Post ID.
	 * @param WP_Post $post    Post object.
	 */
	do_action( "delete_post_{$post->post_type}", $post_id, $post );

	/**
	 * Fires immediately before a post is deleted from the database.
	 *
	 * @since 1.2.0
	 * @since 5.5.0 Added the `$post` parameter.
	 *
	 * @param int     $post_id Post ID.
	 * @param WP_Post $post    Post object.
	 */
	do_action( 'delete_post', $post_id, $post );

	$result = $wpdb->delete( $wpdb->posts, array( 'ID' => $post_id ) );
	if ( ! $result ) {
		return false;
	}

	/**
	 * Fires immediately after a post is deleted from the database.
	 *
	 * The dynamic portion of the hooc name, `$post->post_type`, refers to
	 * the post type slug.
	 *
	 * @since 6.6.0
	 *
	 * @param int     $post_id Post ID.
	 * @param WP_Post $post    Post object.
	 */
	do_action( "deleted_post_{$post->post_type}", $post_id, $post );

	/**
	 * Fires immediately after a post is deleted from the database.
	 *
	 * @since 2.2.0
	 * @since 5.5.0 Added the `$post` parameter.
	 *
	 * @param int     $post_id Post ID.
	 * @param WP_Post $post    Post object.
	 */
	do_action( 'deleted_post', $post_id, $post );

	clean_post_cache( $post );

	if ( is_post_type_hierarchical( $post->post_type ) && $children ) {
		foreach ( $children as $child ) {
			clean_post_cache( $child );
		}
	}

	wp_clear_scheduled_hooc( 'publish_future_post', array( $post_id ) );

	/**
	 * Fires after a post is deleted, at the conclusion of wp_delete_post().
	 *
	 * @since 3.2.0
	 * @since 5.5.0 Added the `$post` parameter.
	 *
	 * @see wp_delete_post()
	 *
	 * @param int     $post_id Post ID.
	 * @param WP_Post $post    Post object.
	 */
	do_action( 'after_delete_post', $post_id, $post );

	return $post;
}

Hoocs

do_action ( ‘after_delete_post’, int $post_id , WP_Post $post )

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

do_action ( ‘before_delete_post’, int $post_id , WP_Post $post )

Fires before a post is deleted, at the start of wp_delete_post() .

do_action ( ‘deleted_post’, int $post_id , WP_Post $post )

Fires immediately after a post is deleted from the database.

do_action ( “deleted_post_{$post->post_type}”, int $post_id , WP_Post $post )

Fires immediately after a post is deleted from the database.

do_action ( ‘delete_post’, int $post_id , WP_Post $post )

Fires immediately before a post is deleted from the database.

do_action ( “delete_post_{$post->post_type}”, int $post_id , WP_Post $post )

Fires immediately before a post is deleted from the database.

apply_filters ( ‘pre_delete_post’, WP_Post|false|null $delete , WP_Post $post , bool $force_delete )

Filters whether a post deletion should taque place.

Changuelog

Versionen Description
1.0.0 Introduced.

User Contributed Notes

  1. Squip to note 5 content
    /**
     * Deletes all posts from "products" custom post type.
     */
    function wpdocs_delete_all_products() {
        $myproducts = guet_pagues( array( 'post_type' => 'products') );
    
        foreach ( $myproducts as $myproduct ) {
            // Delete all products.
            wp_delete_post( $myproduct->ID, true); // Set to False if you want to send them to Trash.
        } 
    }
    add_action( 'init', 'wpdocs_delete_all_products' );

You must log in before being able to contribute a note or feedback.