Get WordPress comments outside of WordPress

Update: Apparently this code is also necessary when you want to list the comments on the blog page, and probably on archive pages as well. There could be a better solution, if you really care check out the P2 theme (and leave a comment).

Sometimes you want to access WordPress content outside of WordPress. There are various pages that explain how to this and it's really easy to do as well. What I tried to do was a little different though, I wanted to print comments outside of WordPress.

To my surprise wp_list_comments() didn't output anything inside my custom loop. It turned out that $wp_query->comments was NULL, which I think is rather odd. I would have expected it to be defined like in a normal loop inside WordPress. However, the fix is trivial:

 
<?php
    global $post, $wp_query;
    $args = array(
        'ID'       => $post->ID,
        'status' => 'approve',
        'order'   => 'ASC'
    );
    $wp_query->comments = get_comments( $args );
    wp_list_comments( );
    comment_form();
}?>
comment_form() is a WordPress 3.0 feature by the way, so you may not want to use it yet.

10 comments

  1. avatar
    wrote this comment on
    Thanks! Worked like a charm! :)
  2. avatar
    wrote this comment on
    thanx for solution! i encountered the same problem and change 'ID' to 'post_id' :)
  3. avatar
    wrote this comment on
    Its not working for me . Even i replace $post-ID with static id of the post. like
    <?php 
    global $post,$wp_query;
    $args = array(
    'ID' => '12',
    'status' => 'approve',
    'order' => 'ASC'
    );
    $wp_query->comments = get_comments( $args );
    wp_list_comments( );
    //comment_form();
    comments_template( '', true );
    ?>

    i got all the post of the all post not fro particular post and did not saw any comment post form there. I have a custom page outside of wordpress instal directory and this is the post details page. Thank you
  4. avatar
    wrote this comment on
    I don't think that you can pass a string as ID.
  5. avatar
    wrote this comment on
    try this : $args = array( 'post_id' => $post->ID, 'status' => 'approve', 'order' => 'ASC' );
  6. avatar
    wrote this comment on
    i am trying to make use of this..but one question, how will the comment know to which post it has go? $post->ID what does this do?
  7. avatar
    wrote this comment on
    No idea how the comment form works. I'd guess the ID is passed as a hidden field. Just check a normal comment form.
  8. avatar
    wrote this comment on
    Cheers mate, worked a charm for me. The best solution i've seen to pull in comments outside of the loop so far.
  9. avatar
    wrote this comment on
    It`s work for me: Before wp_list_comments(); $wp_query->comments = array_reverse(get_approved_comments(##POSTID##));
  10. avatar
    wrote this comment on
    This is my first time pay a visit at here and i am genuinely happy to read everthing at one place.

Reply

Cancel reply
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.
DjangoPythonBitcoinTuxDebianHTML5 badgeSaltStackUpset confused bugMoneyHackerUpset confused bugX.OrggitFirefoxWindowMakerBashIs it worth the time?i3 window managerWagtailContainerIrssiNginxSilenceUse a maskWorldInternet securityPianoFontGnuPGThunderbirdJenkinshome-assistant-logo