WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Like the title says: How can i prevent Wordpress from deleting post comments when i delete a post?

What i am trying to accomplish is that i want to merge all comments from different posts to posts that are relative to eachother by posttitle or a predefined meta key in the post. Sometimes i delete a post that is absolete and i still want those comments to show up for the undeleted posts.

I already have some of the comment aggregate coding for my functions.php. (still has to be adjusted a little bit for the query part to get results based on comment meta instead of the comment post id).

function multiple_comment_post_id_query_filter( $query )
    //get postid's from comments where a certain meta value is set. Database table wp_commentmeta
    //the meta value is extracted from the post meta value with the same id
    //when someone adds a comment, the comment meta will be included
    //put the captured post id's into an array with variable $post_ids 
    $post_ids = array ( 1, 2, 3, 4 );
    if ( FALSE === strpos( $query, 'comment_post_ID = ' ) )
        return $query; // not the query we want to filter

    remove_filter( 'query', 'multiple_comment_post_id_query_filter' );

    $replacement = 'comment_post_ID IN(' . implode( ',', $post_ids ) . ')';
    return preg_replace( '~comment_post_ID = \d+~', $replacement, $query );
add_filter( 'query', 'multiple_comment_post_id_query_filter' );

I prefer not to edit core files in case i have to upgrade (if there is no other possible way i will do it...)

share|improve this question
Edit: i just found out how to override functions. take a look here: catswhocode.com/blog/how-to-overwrite-wordpress-core-functions I also found out by testing the above script that the comments do not get deleted. – jochem Dec 4 '12 at 21:14
Did you solved this? And I really don't get it: what does the code you posted has to do with deleting comments after a post has been deleted? – brasofilo Dec 23 '12 at 0:21
The code merges all comments from the post id's i specify in the array. I thought it would be of use for someone if i posted what i already have. I also said that it is the aggregate function. – jochem Dec 23 '12 at 11:31
What should happen to the comments after the parent post was deleted? Unless they have a parent post they are invisible. – toscho Jan 5 '13 at 2:12
The comments should stay in the database, i aggregate them with other comments which share the same meta value and they show all together on 1 relevant page. I intend to use it for a product comparison website. – jochem Jan 5 '13 at 11:18

To prevent the comment deletion hook into before_delete_post, and filter the query for associated comments so the deletion routine cannot find and delete those.

PHP 5.3 required:

add_action( 'before_delete_post', function( $post_id ) {
    add_filter( 'query', function( $query ) use ( $post_id ) {
        $find = 'WHERE comment_parent = ';
        FALSE !== strpos( $query, $find . $post_id )
            and $query = str_replace( $find . $post_id, $find . '-1', $query );

        return $query;

Here is a noisy old-school version:

    array ( 'T5_Prevent_Comment_Deletion', 'start' )

class T5_Prevent_Comment_Deletion
    protected static $post_id = 0;

    public static function start( $post_id )
        self::$post_id = $post_id;
        add_filter( 'query', array ( __CLASS__, 'hide_comments' ) );

    public function hide_comments( $query )
        $find = 'WHERE comment_parent = ' . self::$post_id;

        if ( FALSE !== strpos( $query, $find ) )
            $query = str_replace( $find, 'WHERE comment_parent = -1', $query );
            remove_filter( 'query', array ( __CLASS__, 'hide_comments' ) );

        return $query;
share|improve this answer
+1 for showing both methodologies, nice! – userabuser Jan 6 '13 at 4:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.