• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress core a2z

WordPress core a2z

WordPress core only

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / wp_trash_post() – Move a post or page to the Trash

You appear to be a bot. Output may be restricted

Description

Move a post or page to the Trash

If Trash is disabled, the post or page is permanently deleted.

Usage

$WP_Post|false|null = wp_trash_post( $post_id );

Parameters

$post_id
( int ) optional – Optional. Post ID. Default is ID of the global $post if EMPTY_TRASH_DAYS equals true.

Returns

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

Source

File name: wordpress/wp-includes/post.php
Lines:

1 to 65 of 65
function wp_trash_post( $post_id = 0 ) {
  if ( ! EMPTY_TRASH_DAYS ) {
    return wp_delete_post( $post_id, true );
  }

  $post = get_post( $post_id );

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

  if ( 'trash' === $post->post_status ) {
    return false;
  }

  
/**
 * Filters whether a post trashing should take place.
 *
 * @since 4.9.0
 *
 * @param bool|null $trash Whether to go forward with trashing.
 * @param WP_Post   $post  Post object.
 */
  $check = apply_filters( 'pre_trash_post', null, $post );

  if ( null !== $check ) {
    return $check;
  }

  
/**
 * Fires before a post is sent to the Trash.
 *
 * @since 3.3.0
 *
 * @param int $post_id Post ID.
 */
  do_action( 'wp_trash_post', $post_id );

  add_post_meta( $post_id, '_wp_trash_meta_status', $post->post_status );
  add_post_meta( $post_id, '_wp_trash_meta_time', time() );

  $post_updated = wp_update_post(
    array(
      'ID'          => $post_id,
      'post_status' => 'trash',
    )
  );

  if ( ! $post_updated ) {
    return false;
  }

  wp_trash_post_comments( $post_id );

  
/**
 * Fires after a post is sent to the Trash.
 *
 * @since 2.9.0
 *
 * @param int $post_id Post ID.
 */
  do_action( 'trashed_post', $post_id );

  return $post;
}
 

 View on GitHub View on Trac

Published: 25th November 2019 | Last updated: 9th December 2020

Primary Sidebar

Information

Function name: wp_trash_post
Plugin ref: WordPress
Version: 5.7.1
Sourcefile: wp-includes/post.php
File ref: wp-includes/post.php
Deprecated?: No
API Letters: P,T,W

Footer

WP-a2z
WordPress core a2z
WordPress core only
WordPress 5.7.1
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  core.wp-a2z.org
© Copyright WP-a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites