从数据库中手动删除帖子

时间:2012-04-06 作者:AlxVallejo

不管出于什么原因,我有一篇帖子和一个页面,它们的名字都是一样的,这会导致db挂起。我也无法更改slug,所以我需要手动从数据库中删除帖子并重新开始。

现在,我愿意从wp\\U posts和wp\\U Posteta中调出帖子ID并手动删除每个条目。假设我在帖子或页面上都没有评论,这会破坏数据库中的其他内容吗?

EDIT

我找到了这个脚本,我认为它是可以的,但我不确定这是否考虑到修订或任何其他我会留下的手动删除帖子

DELETE a,b,c
FROM wp_posts a
LEFT JOIN wp_term_relationships b ON ( a.ID = b.object_id )
LEFT JOIN wp_postmeta c ON ( a.ID = c.post_id )
WHERE a.ID = xxx;

5 个回复
最合适的回答,由SO网友:Brian Fegter 整理而成

将其放入插件目录中的文件中,您应该能够使用查询字符串从WP安装中执行此操作。

/*
Plugin Name: Delete Specific Post
Description: Rid the post forever!
Version: 0.1
Author: WPSE
License: GPL2
*/

add_filter(\'query_vars\', \'delete_post_query_var\');
function delete_post_query_var($vars){
    $vars[] = \'delete_post\';
    return $vars;
}

add_action(\'admin_init\', \'manually_delete_post\', 0);
function manually_delete_post(){

    if(!is_user_logged_in())
        return;

    if(!current_user_can(\'manage_options\'))
        return;

    if(get_query_var(\'delete_post\')){
        $id = esc_attr(get_query_var(\'delete_post\'));
        $delete = wp_delete_post($id, true); //True force deletes the post and doesn\'t send it to the Trash
        if($delete)
            echo "Post $id deleted successfully!";
        else
            echo "Post $id was not deleted.";
        exit;
    }
}
您只需确保已登录管理员帐户,然后访问:http://yourdomain.com/wp-admin/?delete_post=POSTID

SO网友:Francis Yaconiello

最近,我有一个帖子类型的问题,我想nuke,到处都是关系,没有DB强制FK级联。

/* DELETE REVISIONS */
DELETE posts
FROM
    `prefix_posts` AS posts
INNER JOIN `prefix_posts` AS parents ON posts.post_parent = parents.ID
WHERE
    parents.post_type = "myposttype"; /*REPLACE W/ parents.ID = "yourID"*/


/* DELETE POSTS */
DELETE
FROM
    `prefix_posts`
WHERE
    post_type = "myposttype"; /*REPLACE W/ ID = "yourID"*/


/* DELETE ORPHANED POST META */
DELETE
FROM
    `prefix_postmeta`
WHERE
`prefix_postmeta`.`post_id` NOT IN(
    SELECT
        `prefix_posts`.`ID`
    FROM
        `prefix_posts`
);
您是否使用任何创建表的插件?(如post-2-post)

/* ORPHANED POST2POSTS */
DELETE
FROM
    `prefix_p2p`
WHERE
    `prefix_p2p`.`p2p_from` NOT IN(
        SELECT
            `prefix_posts`.`ID`
        FROM
            `prefix_posts`
);

DELETE
FROM
`prefix_p2p`
WHERE
    `prefix_p2p`.`p2p_to` NOT IN(
        SELECT
            `prefix_posts`.`ID`
        FROM
            `prefix_posts`
    );
它可能更容易使用内置的WP函数。。。但如果需要SQL调用,请查看上面的内容。

SO网友:Tribalpixel

为什么不简单地使用wp_delete_post() ?

当文章和页面消失时,与之相关的所有内容也会被删除。这包括注释、post元字段以及post和分类术语之间的关系。

SO网友:Eli Jayson

扩展@Brian Fegter的答案Above 您可以使用下面的插件批量删除帖子。它创建了一个WP管理页面,其中包含一个文本区域,用于删除页面/帖子的ID。

<?php

/*
Plugin Name: Delete Posts By ID
Description: Delete posts and pages by id
Version: 0.1
Author: WPSE
License: GPL2
*/


add_action( \'admin_menu\', \'deletePostsMenu\' );

function deletePostsMenu() {
    add_menu_page(
        \'Delete Posts\',
        \'Delete Posts\',
        \'manage_options\',
        \'delete-posts/delete-posts-admin-page.php\',
        \'delete_posts_admin_page\',
        \'dashicons-trash\',
        6
    );
}

function delete_posts_admin_page() {
    ?>
    <div class="wrap">
        <h2>Delete Posts By ID</h2>
        <p>Add post/page ids separated by a line break</p>
        <form action="" method="post">
            <label for="deletePostsByID"></label>
            <textarea name="deletePostsByID" id="ids" cols="30" rows="10"></textarea>
            <input type="submit">
        </form>
    </div>
    <?php

    if (isset($_POST[\'deletePostsByID\'])) {
        delete_posts($_POST[\'deletePostsByID\']);
        return;
    }
}

//add_action(\'admin_init\', \'manually_delete_post\', 0);
function delete_posts($submittedIDs){

    if(!is_user_logged_in())
        return;

    if(!current_user_can(\'manage_options\'))
        return;

   /* // Strip whitespace
    $stripped = str_replace(\' \', \'\', $submittedIDs);*/

    // Set to array
    $idsArray = explode("\\r\\n",$submittedIDs);

    echo \'<ol>\';
    // Delete posts by id
    foreach ($idsArray as $id){
        $delete = wp_delete_post($id, false); //True force deletes the post and doesn\'t send it to the Trash
        if($delete)
            echo "<li>Post $id deleted successfully!</li>";
        else
            echo "<li>Post $id was not deleted.</li>";

    }

    echo \'</ol>\';

    exit;
}

SO网友:Eugene Manuilov

好的,如果你想完全删除它,你必须从中删除帖子/页面wp_term_relationships 桌子但如果问题只存在于slug中,您可以在数据库中更新适当的记录,您的slug就会得到修复。要更新slug,需要运行以下查询:

UPDATE wp_posts SET post_name = \'my-new-slug\' WHERE ID = /*your post ID here*/

结束

相关推荐

cutsom posts 404ing

我无法阻止我的自定义帖子类型偶尔404。它并不总是发生,我也无法预测复制问题-就像有时会发生,有时不会。然而,一旦我清除缓存,404就会消失,URL也会工作。我已尝试重置并重新保存永久链接,它们当前是类别/postname。我也尝试过使用flush重写规则,将它们设置为空白和false,但似乎没有任何方法可以解决这个问题。这是我当前的代码:add_action(\'init\', \'create_raceresults_post_type\'); function create_ra