wp_delete_post example

do_action( 'wpmu_delete_user', $id$user );

    $blogs = get_blogs_of_user( $id );

    if ( ! empty( $blogs ) ) {
        foreach ( $blogs as $blog ) {
            switch_to_blog( $blog->userblog_id );
            remove_user_from_blog( $id$blog->userblog_id );

            $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) );
            foreach ( (array) $post_ids as $post_id ) {
                wp_delete_post( $post_id );
            }

            // Clean links.             $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) );

            if ( $link_ids ) {
                foreach ( $link_ids as $link_id ) {
                    wp_delete_link( $link_id );
                }
            }

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

        if ( 'attachment' === $post->post_type ) {
            $force = ( ! MEDIA_TRASH );
            if ( ! wp_delete_attachment( $post_id$force ) ) {
                wp_die( __( 'Error in deleting the attachment.' ) );
            }
        } else {
            if ( ! wp_delete_post( $post_id, true ) ) {
                wp_die( __( 'Error in deleting the item.' ) );
            }
        }

        wp_redirect( add_query_arg( 'deleted', 1, $sendback ) );
        exit;

    case 'preview':
        check_admin_referer( 'update-post_' . $post_id );

        $url = post_preview();

        
return $menu_item;
        }

        // We don't support trashing for menu items.         if ( ! $request['force'] ) {
            /* translators: %s: force=true */
            return new WP_Error( 'rest_trash_not_supported', sprintf( __( "Menu items do not support trashing. Set '%s' to delete." ), 'force=true' ), array( 'status' => 501 ) );
        }

        $previous = $this->prepare_item_for_response( get_post( $request['id'] )$request );

        $result = wp_delete_post( $request['id'], true );

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

        $response = new WP_REST_Response();
        $response->set_data(
            array(
                'deleted'  => true,
                'previous' => $previous->get_data(),
            )
        );

function wp_delete_post_revision( $revision ) {
    $revision = wp_get_post_revision( $revision );

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

    $delete = wp_delete_post( $revision->ID );

    if ( $delete ) {
        /** * Fires once a post revision has been deleted. * * @since 2.6.0 * * @param int $revision_id Post revision ID. * @param WP_Post $revision Post revision object. */
        do_action( 'wp_delete_post_revision', $revision->ID, $revision );
    }

            }
        }

        break;

    case 'delete-menu-item':
        $menu_item_id = (int) $_REQUEST['menu-item'];

        check_admin_referer( 'delete-menu_item_' . $menu_item_id );

        if ( is_nav_menu_item( $menu_item_id ) && wp_delete_post( $menu_item_id, true ) ) {
            $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __( 'The menu item has been successfully deleted.' ) . '</p></div>';
        }

        break;

    case 'delete':
        check_admin_referer( 'delete-nav_menu-' . $nav_menu_selected_id );

        if ( is_nav_menu( $nav_menu_selected_id ) ) {
            $deletion = wp_delete_nav_menu( $nav_menu_selected_id );
        } else {
            

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;
    }

    
$post_del = get_post( $post_id );

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

                if ( 'attachment' === $post_del->post_type ) {
                    if ( ! wp_delete_attachment( $post_id ) ) {
                        wp_die( __( 'Error in deleting the attachment.' ) );
                    }
                } else {
                    if ( ! wp_delete_post( $post_id ) ) {
                        wp_die( __( 'Error in deleting the item.' ) );
                    }
                }
                $deleted++;
            }
            $sendback = add_query_arg( 'deleted', $deleted$sendback );
            break;
        case 'edit':
            if ( isset( $_REQUEST['bulk_edit'] ) ) {
                $done = bulk_edit_posts( $_REQUEST );

                
$id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0;
    check_ajax_referer( "{$action}_$id);

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

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

    if ( wp_delete_post( $id ) ) {
        wp_die( 1 );
    } else {
        wp_die( 0 );
    }
}

/** * Handles sending a post to the Trash via AJAX. * * @since 3.1.0 * * @param string $action Action to perform. */

    public function update_item( $request ) {
        $template = get_block_template( $request['id']$this->post_type );
        if ( ! $template ) {
            return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.' ), array( 'status' => 404 ) );
        }

        $post_before = get_post( $template->wp_id );

        if ( isset( $request['source'] ) && 'theme' === $request['source'] ) {
            wp_delete_post( $template->wp_id, true );
            $request->set_param( 'context', 'edit' );

            $template = get_block_template( $request['id']$this->post_type );
            $response = $this->prepare_item_for_response( $template$request );

            return rest_ensure_response( $response );
        }

        $changes = $this->prepare_item_for_database( $request );

        if ( is_wp_error( $changes ) ) {
            
do_action( 'xmlrpc_call', 'wp.deletePost', $args$this );

        $post = get_post( $post_id, ARRAY_A );
        if ( empty( $post['ID'] ) ) {
            return new IXR_Error( 404, __( 'Invalid post ID.' ) );
        }

        if ( ! current_user_can( 'delete_post', $post_id ) ) {
            return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this post.' ) );
        }

        $result = wp_delete_post( $post_id );

        if ( ! $result ) {
            return new IXR_Error( 500, __( 'Sorry, the post could not be deleted.' ) );
        }

        return true;
    }

    /** * Retrieves a post. * * @since 3.4.0 * * The optional $fields parameter specifies what fields will be included * in the response array. This should be a list of field names. 'post_id' will * always be included in the response regardless of the value of $fields. * * Instead of, or in addition to, individual field names, conceptual group * names can be used to specify multiple fields. The available conceptual * groups are 'post' (all basic fields), 'taxonomies', 'custom_fields', * and 'enclosure'. * * @see get_post() * * @param array $args { * Method arguments. Note: arguments must be ordered as documented. * * @type int $0 Blog ID (unused). * @type string $1 Username. * @type string $2 Password. * @type int $3 Post ID. * @type array $4 Optional. The subset of post type fields to return. * } * @return array|IXR_Error Array contains (based on $fields parameter): * - 'post_id' * - 'post_title' * - 'post_date' * - 'post_date_gmt' * - 'post_modified' * - 'post_modified_gmt' * - 'post_status' * - 'post_type' * - 'post_name' * - 'post_author' * - 'post_password' * - 'post_excerpt' * - 'post_content' * - 'link' * - 'comment_status' * - 'ping_status' * - 'sticky' * - 'custom_fields' * - 'terms' * - 'categories' * - 'tags' * - 'enclosure' */
'rest_user_cannot_delete_post',
                __( 'Sorry, you are not allowed to delete this post.' ),
                array( 'status' => rest_authorization_required_code() )
            );
        }

        $request->set_param( 'context', 'edit' );

        // If we're forcing, then delete permanently.         if ( $force ) {
            $previous = $this->prepare_item_for_response( $post$request );
            $result   = wp_delete_post( $id, true );
            $response = new WP_REST_Response();
            $response->set_data(
                array(
                    'deleted'  => true,
                    'previous' => $previous->get_data(),
                )
            );
        } else {
            // If we don't support trashing for this type, error out.             if ( ! $supports_trash ) {
                return new WP_Error(
                    
$post_id = (int) $post['post_id'];
        if ( ! $post_id ) {
            continue;
        }

        $del_post = get_post( $post_id );

        if ( ! $del_post || 'trash' !== $del_post->post_status ) {
            delete_post_meta( $post_id, '_wp_trash_meta_status' );
            delete_post_meta( $post_id, '_wp_trash_meta_time' );
        } else {
            wp_delete_post( $post_id );
        }
    }

    $comments_to_delete = $wpdb->get_results( $wpdb->prepare( "SELECT comment_id FROM $wpdb->commentmeta WHERE meta_key = '_wp_trash_meta_time' AND meta_value < %d", $delete_timestamp ), ARRAY_A );

    foreach ( (array) $comments_to_delete as $comment ) {
        $comment_id = (int) $comment['comment_id'];
        if ( ! $comment_id ) {
            continue;
        }

        
'%d requests marked as complete.',
                            $count
                        ),
                        $count
                    ),
                    'success'
                );
                break;

            case 'delete':
                foreach ( $request_ids as $request_id ) {
                    if ( wp_delete_post( $request_id, true ) ) {
                        $count++;
                    } else {
                        $failures++;
                    }
                }

                if ( $failures ) {
                    add_settings_error(
                        'bulk_action',
                        'bulk_action',
                        sprintf(
                            

function wp_delete_nav_menu( $menu ) {
    $menu = wp_get_nav_menu_object( $menu );
    if ( ! $menu ) {
        return false;
    }

    $menu_objects = get_objects_in_term( $menu->term_id, 'nav_menu' );
    if ( ! empty( $menu_objects ) ) {
        foreach ( $menu_objects as $item ) {
            wp_delete_post( $item );
        }
    }

    $result = wp_delete_term( $menu->term_id, 'nav_menu' );

    // Remove this menu from any locations.     $locations = get_nav_menu_locations();
    foreach ( $locations as $location => $menu_id ) {
        if ( $menu_id == $menu->term_id ) {
            $locations[ $location ] = 0;
        }
    }

        $post_types_to_delete = apply_filters( 'post_types_to_delete_with_user', $post_types_to_delete$id );
        $post_types_to_delete = implode( "', '", $post_types_to_delete );
        $post_ids             = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d AND post_type IN ('$post_types_to_delete')", $id ) );
        if ( $post_ids ) {
            foreach ( $post_ids as $post_id ) {
                wp_delete_post( $post_id );
            }
        }

        // Clean links.         $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) );

        if ( $link_ids ) {
            foreach ( $link_ids as $link_id ) {
                wp_delete_link( $link_id );
            }
        }
    }
Home | Imprint | This part of the site doesn't use cookies.