Convert comments to answers?

3.75K viewsGeneral
0

hello, I have a webpage based on wordpress with a lot of posts and post’s comments (1000+ posts, 2000+ comments).
Is there any way to convert comments to answers?

Answered question

what is post_type?

comments are one the comments table on wordpress database.

I know that. what is your post_type? post?

yes, post_type is “post”

0

can you give us the SQL code to convert comments to answers via phpMyAdmin?
My theme has a very strange functions.php file and i think it’s easier to copy/convert the data via phpMyAdmin if is possible…..

Posted new comment

ok….i did it working with functions.php file.
It’s not necessary to give us the SQL code.

0

MAKE SURE TO KEEP A BACKUP OF YOUR SITE BEFORE RUNNING THIS.
Here is the simplest version of code. Add it to functions.php and refresh the page until all comments are gone. And make sure to remove the code afterward and run the recount in AnsPress tools.

<?php
  global $wpdb;
  $sql = "SELECT * FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' AND post_type='post' ORDER BY comment_date_gmt DESC LIMIT 100";
  $comments = $wpdb->get_results( $sql );
 if ( $comments ) {
    foreach ( $comments as $c ) {
        wp_insert_post( array(
            'post_type'    => 'answer',
            'post_status'  => 'publish',
            'post_parent'  => $c->comment_post_ID,
            'post_content' => $c->comment_content,
            'post_author'  => $c->user_id,
        ) );
                 // Delete the comment after answer is created.
        wp_delete_comment( $c->comment_ID, true );
    }
}

Posted new comment

thanks for the answer, but i don’t know where to add the code
1) to wordpress’s functions.php
2) to my theme’s functions.php
3) to anspress’s function.php

please inform me about this: 1 or 2 or 3

Theme functions.php. wrap this inside a init hook. like:

add_action( ‘init’, function(){
// copy the codes from above to here.
});