3

I'm switching to a new theme which uses the custom post types: Question (as posts) and Answer (as comments to posts). I have converted current Posts into Question using a plugin. Now, I have to convert comments (of each post) to Answer which will be relate to each (converted) Question.

I used the following code but although all comments converted and created the right relation to each question, I finally got multiple answer depending on how many comments where in each post. To be more clear, if a question had 5 comments before, now I have 5x5=25 answers. The code actually inserted the answers 5 times instead of just 1.

I think that the problem is in the foreach loop. Can you please help how should I modify the code to avoid this problem?

$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' => 'answer', // Set post type, post type must be answer.
'post_parent' => $thread->ID // Set id of question to make relation between question and answer
'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.

endforeach;
endforeach;
10
  • Are you saying that the code actually inserted 25 rather than the expected 5 in the database, or that it is merely displaying answers 5 times instead of 1 time each? If the latter, problem could be somewhere in loop/template...
    – CK MacLeod
    Commented Jun 1, 2016 at 23:18
  • @CKMacLeod it actually inserted 25 rather than the expected 5 in the database. Commented Jun 2, 2016 at 6:40
  • Why not put facebook cimments? That will push your traffic up. Commented Jun 2, 2016 at 6:45
  • seems strange, maybe try adding $thread_comments = array_unique($thread_comments); before the second loop...
    – majick
    Commented Jun 2, 2016 at 9:15
  • Not really sure why the nested foreach loop as you've written it is not getting the expected behavior. However, a general search for nested foreach loops does reveal numerous indications of problems, and. when I've tested alternatives also involving nested foreach loops, I've gotten very unexpected behavior - including both failure to complete the operation and completely unexpected content. I wonder if part of the problem has to do with mixing arrays and objects, but that's just a guess.
    – CK MacLeod
    Commented Jun 2, 2016 at 16:49

1 Answer 1

1

Please try:

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

$thread_posts = get_posts( $args ); // Get all posts of thread.

foreach($thread_posts as $thread_post_object) {

    /*not sure really need to be working IDs only, but is simpler, plus made checking was getting right ones easier */
    $thread_post_IDs[] = $thread_post_object->ID; 

}

foreach ($thread_post_IDs as $thread_post_ID) {


    $comments_query = new WP_Comment_Query;
    $thread_comments = $comments_query->query(array 
        (
            'post_id' => $thread_post_ID,
            'status' => 'approve'
        )
    );

    foreach ($thread_comments as $thread_comment) {
        $reply_post = array(
            'post_status' => 'publish', // Set replies status.
            'post_type' => 'answer', // Set post type, post type must be answer.
            'post_parent' => $thread_post_ID, // Set id of question to make relation between question and answer
            'post_author' => $thread_comment->user_id, // Set comment user id as post id - requires registered users otherwise returns 0
            'post_title' => 'RE: ' . get_the_title($thread_post_ID), //since have ID only need alternative way to get title
            'post_content' => $thread_comment->comment_content // Set comment content as post content.
        );

        //print_r($reply_post); // if you want to test it first
        wp_insert_post($reply_post); // if you want to see what happens!

    }

}
3
  • So, here's what happened. I run the script at first using only the print_r and the output was correct. Then, I run the wp_insert_post and noticed that this causes the problem. It actually inserts every comment 5 times no matter how comments a post has. At least we found what causes the trouble, any ideas on how to solve this? Commented Jun 3, 2016 at 19:15
  • I'll look into it later if no one trots a long with an answer. Have you tried Googling for wp_insert_post problems?
    – CK MacLeod
    Commented Jun 3, 2016 at 20:07
  • No need to look it further, it finally worked! I created a new custom template page with only the script and it inserted/coverted all the comments as it should. Somehow, there was a conflict when other elements loaded in my first attempts. A huge thanks for your kind help! Commented Jun 3, 2016 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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