Forums

[resolved] How to Mass Delete Comments from Closed WordPress Topics? (6 posts)

  1. WordPress User
    Member
    Posted 2 years ago #

    Hello everyone

    I wanted to know, how to delete comments from all closed topics in a wordpress blog?

    To clear the question, suppose we have 10 closed topics in a wordpress blog and want to remove all comments from those 10 topics in a single step, how to do it?

    Can anyone post the exact MySQL query for it?

    Thanks in advance.

  2. James
    Happiness Engineer
    Posted 2 years ago #

    If you go to the Comments section in the admin panel, you can search for the title of the posts that you closed. It will find all comments relating to the post title, but it will also find a few that are just close matches. It's not perfect, but should get the job done. Once your search is done, it's just a matter of checking the comments and hitting the delete button.

  3. WordPress User
    Member
    Posted 2 years ago #

    ^^ Thanks for the reply. Actually thats what I'm doing currently. One can directly edit all comments in a particular topic by using following URL:

    http://www.site_url.com/wp-admin/edit-comments.php?p=topic_ID

    But I want to delete comments from all closed topics in a single step. Its not easy to find out which topics are closed, so a MySQL query will really help a lot.

  4. WordPress User
    Member
    Posted 2 years ago #

    I apologize if bumping topics is not allowed in this forum as my previous comment got deleted by moderators.

    I'm really surprised to see that no one except one moderator has replied in this topic yet.

    I'll really appreciate if some one provides a working MySQL query for the desired task.

    Thanks in advance.

    PS: I'm bumping this topic because I really need the query script urgently.

  5. Mark / t31os
    Moderator
    Posted 2 years ago #

    Provided as is, backup beforehand if in doubt..

    MySQL query

    DELETE c
    FROM wp_posts p
    LEFT JOIN wp_comments c ON c.comment_post_ID = p.ID
    WHERE p.comment_count > 0
    AND p.comment_status = 'closed'

    Using WordPress wpdb class

    $r = $wpdb->get_results( "DELETE c
    FROM $wpdb->posts p
    LEFT JOIN $wpdb->comments c ON c.comment_post_ID = p.ID
    WHERE p.comment_count > 0
    AND p.comment_status = 'closed'");

    Queries the posts table for posts with a comment count more then 0(it's the last column of the posts table), joins the comments table matching against the post's ID and deletes the matching comments.

    Only tested very briefly on a local installation(2.9.2), so i can't say for sure if it's perfect, so use at your own risk.

    NOTE: My testing was done in PHPMYADMIN, so i'm not sure if the wpdb method above is correct, but it's there simply to show how it might look inside a wpdb method..

  6. WordPress User
    Member
    Posted 2 years ago #

    ^^ Thank you so much for your reply. I would highly appreciate if you also reply in following topic:

    http://wordpress.org/support/topic/398933?replies=1

Topic Closed

This topic has been closed to new replies.

About this Topic