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

I've never messed with the comments loop before, but I'm trying something out here. Basically, I have my comments form in a popup iframe, an ajax action which handles the form submission, inserts the comment, and updates the list of comments.

I'm having a problem running the comments loop a second time on the page, though. The comments form comes through fine, but the comments don't show on the Ajax refresh. have_comments() turns up false, and wp_list_comments() outputs nothing.

Any ideas what's going wrong?

Here's my Ajax handler, in case there's an issue in there:

/* Submit Comments Through AJAX */

add_action ( 'wp_ajax_submit_comment', 'ajax_insert_comment' );
add_action ( 'wp_ajax_nopriv_submit_comment', 'ajax_insert_comment' );

function ajax_insert_comment() {

    /* obviously all of this needs sanitization, 
       its oversimplified, just to get the idea across */
    $data = extract( wp_parse_args( $_POST ) );
    $current_user = wp_get_current_user();
    $comment = wp_insert_comment( array(
        'user_id' => $current_user->ID,
        'comment_author' => $current_user->display_name,
        'comment_author_email' => $current_user->user_email,
        'comment_post_ID' => intval( $comment_post_ID ),
        'comment_parent' => intval( $comment_parent ),
        'comment_content' => $comment,
        'comment_type' => $comment_type
        ) );

    global $post; $wp_the_query;

    $post = get_post( $comment_post_ID );
    setup_postdata( $post );

    /* Here's the problem: How to rewind the comments query 
        so that it will actually display comments. I've tried 
        all of these things, but they're not working right now */
    $wp_query->current_comment = -1;

    // send comments list again.
    include( get_stylesheet_directory(). '/comments.php' );


The callback just takes the Ajax reponse and updates the comments div with the result. But its missing the whole comment list. Any ideas what I need to call to rewind the comments loop and make it actually display?

share|improve this question
whats your comments.php like? – Bainternet Jun 14 '11 at 9:16
The comments.php is basically just taken straight from twentyten. I overwrote the twentyten_callback function, buts the only real modification I've made. – goldenapples Jun 14 '11 at 16:46
up vote 1 down vote accepted

Ajax handler runs in completely separate instance of WordPress, it has no access to global variable from your pages and such.

Basically there is nothing to rewind here, you need to pass those environment variables in request and query for comments from scratch.

share|improve this answer
Ah, I had figured that one out. A real "duh" moment for me. Thanks for posting a response, though. – goldenapples Jun 23 '11 at 0:08

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.