1

Currently, I use WordPress as a Q&A website where users can register, ask questions and give answers as well. I use the AskIt theme from ElegantThemes which uses the default WordPress Post Types (Posts, Pages, Comments, etc.). There are about 300 registered users, more than 350 questions and 1000 answers/comments right now.

The new theme I want to migrate to (ForumEngine), adds/uses Custom Post Types for the Questions (instead of the current Posts) and Answers (instead of the current comments). Also, the 'blog' section, which co-exists with Custom Post Types, use the default WP Post types (Posts, comments).

I managed to convert the default Post type to Custom Post Type (Questions) with the help of a plugin. My problem is that I don't know how can I convert the comments to the Custom Post Type (Answers) and since the fact that comments are not Post Types apparently this one might be tricky.

When I go to the Edit page of a converted CPT post, I can see the comments at the bottom but they are not showing in the frontend. So, they are still linked to each post but couldn't figure out how to convert them to the CPT.

Can you please help me figure out how can I do this?

I already did a further research and asked theme's creators for help but this is beyond their scope of support.

  • I don't understand convert the comments to the Custom Post Type (Answers). What do you mean by custom post type comments. Comments only relate with posts and the post belong to any post type. Please explain briefly and create structure for it. THANKS :) – ksr89 May 25 '14 at 4:39
  • Have a look at this tutorial and this one. Might be of some help – Pieter Goosen May 25 '14 at 6:43
  • @ksr89 Right now there are Posts and Comments that belong/linked to Posts, that's the default. Posts will be converted to a Custom Post Type named Threads. Comments will be coverted also to a Custom Post Type named Replies so that user can reply to a CPT Post (outside the loop I think is the right term). So, default WP Posts and Comments will work as usual for the 'blog' section of the 'forum' and the two CPT will handle the questions and answers of users. – John Enxada May 25 '14 at 14:06
  • You can take a look at the demo theme to fully understand the structure here: demo.enginethemes.com/forumengine/wp-admin/… or see this picture: imgur.com/nHH21bM – John Enxada May 25 '14 at 14:07
0

OK @John Enxada i looked in the theme admin part. The thing which you are asking is difficult but possible. The first thing you have to check that how they are creating relation between two post types i.e relation between thread and replies. I have no excess to the database(can't see in database to check how they relate). You have to do it by yourself.

For this you have to create a script yourself. I can only help you how to create it. First get the backup of your database so that if anything go wrong you have a backup.

Once you know then you can change the post type of post to thread. Which you did already. Then you can get all the posts of thread using get_posts function. Then using a for/foreach loop get all the comments of each post one by one using get_comments. Again apply a for/foreach loop for each comment one by one. Then using wp_insert_post insert these comments as a post having post_type = replies.

Here is an code that will help you somehow.

$args = array(
    'posts_per_page'   => -1,
    'post_type'        => 'thread'
);

$threads = get_posts( $args ); // Get all posts of thread.
foreach($threads as $thread):
    $comment_args = array(
        'post_id' => $thread->ID
    );
    $thread_comments = get_comments($comment_args); // Get all comments of the post.
    foreach($thread_comments as $thread_comment):
        $reply_post = array(
            'post_status'  => 'publish', // Set replies status.
            'post_type'    => 'replies', // Set post type.
            'post_author'  => $thread_comment->user_id, // Set comment user id as post id.
            'post_title'   => 'RE: '.$thread->post_title, //They prefix RE: to every reply so i think we might be do the same.
            'post_content' => $thread_comment->comment_content // Set comment content as post content.
        );
        wp_insert_post($reply_post); // Insert the comment as post having post type replies.
        // Write the code through which they create relation between these post types
    endforeach;
endforeach;

These are some important value which a post must require while inserting. But i think they are inserting some more values as post_meta. So you have to look onto those values also. Once you start the script it will convert all your comments to posts having post_type = replies.

  • That's a very helpfull answer @ksr89 and I thank you for that. I can understand your steps but I don't know enough to execute them, so please excuse my followup questions. I know that I have to check into phpmyadmin to find the relation about the two CPT but which fields should I search for? How can I run the script and where to put your code? – John Enxada May 25 '14 at 21:40
  • For finding the field go for testing phase. Create a thread and check in database which field create, after that create a reply for that and check which field are created and which fields are affected in the thread. You can write the script on any page whether on home page on create a new template because the script is execute only one time. You check whether the script is working properly on localhost. Once you fully satisfied that everything is working properly use searchandreplacedb script and change localhost to your website name. Here`s link and yout databse is ready for online. – ksr89 May 26 '14 at 5:37
  • The theme is not available in trail version so i can't download it and check everything. Sorry for that. – ksr89 May 26 '14 at 5:40
  • your answer is really helpful. Although the theme's creators announced that they will release a new theme that fits my needs and I will migrate to the new one. So, I will put all this on hold so I can see how thing work on the new theme first, maybe it'll be similar method to migrate data. Thanks again. – John Enxada May 30 '14 at 0:38
  • 1
    So, an update (after 2 years lol). The following code is the right one which makes the correct relation between comments and posts. – John Enxada Jun 1 '16 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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