wp_trash_post example

if ( ! current_user_can( 'delete_post', $post_id ) ) {
            wp_die( __( 'Sorry, you are not allowed to move this item to the Trash.' ) );
        }

        $user_id = wp_check_post_lock( $post_id );
        if ( $user_id ) {
            $user = get_userdata( $user_id );
            /* translators: %s: User's display name. */
            wp_die( sprintf( __( 'You cannot move this item to the Trash. %s is currently editing.' )$user->display_name ) );
        }

        if ( ! wp_trash_post( $post_id ) ) {
            wp_die( __( 'Error in moving the item to Trash.' ) );
        }

        wp_redirect(
            add_query_arg(
                array(
                    'trashed' => 1,
                    'ids'     => $post_id,
                ),
                $sendback
            )
        );
break;

        case 'trash':
            if ( empty( $post_ids ) ) {
                break;
            }
            foreach ( $post_ids as $post_id ) {
                if ( ! current_user_can( 'delete_post', $post_id ) ) {
                    wp_die( __( 'Sorry, you are not allowed to move this item to the Trash.' ) );
                }

                if ( ! wp_trash_post( $post_id ) ) {
                    wp_die( __( 'Error in moving the item to Trash.' ) );
                }
            }
            $location = add_query_arg(
                array(
                    'trashed' => count( $post_ids ),
                    'ids'     => implode( ',', $post_ids ),
                ),
                $location
            );
            break;
        
global $wpdb;

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

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

    $post = get_post( $post );

    if ( ! $force_delete && ( 'post' === $post->post_type || 'page' === $post->post_type ) && 'trash' !== get_post_status( $postid ) && EMPTY_TRASH_DAYS ) {
        return wp_trash_post( $postid );
    }

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

    /** * Filters whether a post deletion should take 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. */
check_ajax_referer( "{$action}_$id);

    if ( ! current_user_can( 'delete_post', $id ) ) {
        wp_die( -1 );
    }

    if ( ! get_post( $id ) ) {
        wp_die( 1 );
    }

    if ( 'trash-post' === $action ) {
        $done = wp_trash_post( $id );
    } else {
        $done = wp_untrash_post( $id );
    }

    if ( $done ) {
        wp_die( 1 );
    }

    wp_die( 0 );
}

return new WP_Error(
                    'rest_already_trashed',
                    __( 'The post has already been deleted.' ),
                    array( 'status' => 410 )
                );
            }

            /* * (Note that internally this falls through to `wp_delete_post()` * if the Trash is disabled.) */
            $result   = wp_trash_post( $id );
            $post     = get_post( $id );
            $response = $this->prepare_item_for_response( $post$request );
        }

        if ( ! $result ) {
            return new WP_Error(
                'rest_cannot_delete',
                __( 'The post cannot be deleted.' ),
                array( 'status' => 500 )
            );
        }

        
/* * Actually, in lieu of keeping alive, trash any customization drafts here if the changeset itself is * getting trashed. This is needed because when a changeset transitions to a draft, then any of the * dependent auto-draft post/page stubs will also get transitioned to customization drafts which * are then visible in the WP Admin. We cannot wait for the deletion of the changeset in which * _wp_delete_customize_changeset_dependent_auto_drafts() will be called, since they need to be * trashed to remove from visibility immediately. */
    if ( 'trash' === $new_status ) {
        foreach ( $data['nav_menus_created_posts']['value'] as $post_id ) {
            if ( ! empty( $post_id ) && 'draft' === get_post_status( $post_id ) ) {
                wp_trash_post( $post_id );
            }
        }
        return;
    }

    $post_args = array();
    if ( 'auto-draft' === $new_status ) {
        /* * Keep the post date for the post matching the changeset * so that it will not be garbage-collected before the changeset. */
        
if ( empty( $data['nav_menus_created_posts']['value'] ) ) {
        return;
    }
    remove_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' );
    foreach ( $data['nav_menus_created_posts']['value'] as $stub_post_id ) {
        if ( empty( $stub_post_id ) ) {
            continue;
        }
        if ( 'auto-draft' === get_post_status( $stub_post_id ) ) {
            wp_delete_post( $stub_post_id, true );
        } elseif ( 'draft' === get_post_status( $stub_post_id ) ) {
            wp_trash_post( $stub_post_id );
            delete_post_meta( $stub_post_id, '_customize_changeset_uuid' );
        }
    }
    add_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' );
}

/** * Handles menu config after theme change. * * @access private * @since 4.9.0 */
return new WP_Error(
                    'rest_template_already_trashed',
                    __( 'The template has already been deleted.' ),
                    array( 'status' => 410 )
                );
            }

            /* * (Note that internally this falls through to `wp_delete_post()` * if the Trash is disabled.) */
            $result           = wp_trash_post( $id );
            $template->status = 'trash';
            $response         = $this->prepare_item_for_response( $template$request );
        }

        if ( ! $result ) {
            return new WP_Error(
                'rest_cannot_delete',
                __( 'The template cannot be deleted.' ),
                array( 'status' => 500 )
            );
        }

        
foreach ( (array) $post_ids as $post_id ) {
                if ( ! current_user_can( 'delete_post', $post_id ) ) {
                    wp_die( __( 'Sorry, you are not allowed to move this item to the Trash.' ) );
                }

                if ( wp_check_post_lock( $post_id ) ) {
                    $locked++;
                    continue;
                }

                if ( ! wp_trash_post( $post_id ) ) {
                    wp_die( __( 'Error in moving the item to Trash.' ) );
                }

                $trashed++;
            }

            $sendback = add_query_arg(
                array(
                    'trashed' => $trashed,
                    'ids'     => implode( ',', $post_ids ),
                    'locked'  => $locked,
                ),
Home | Imprint | This part of the site doesn't use cookies.