List Comments in get_posts Loop
-
Ah, this old chestnut. The issue is with
setup_postdata( $wod );. When usingsetup_postdata()the variable passed to it must be$post, and you need to load the global post variable.So you segment of code would become:
$args = array ( 'post_type' => array( 'wod' ), 'order' => 'ASC', 'orderby' => 'menu_order', 'post_per_page' => 999 ); $wods = get_posts( $args ); global $post; ?> <?php foreach ( $wods as $post ) : setup_postdata( $post ); if ( comments_open($wod->ID) || get_comments_number($wod->ID) ) { comment_form(); global $wp_query; $comment_args = array( 'ID' => $post->ID, 'status' => 'approve', 'order' => 'ASC' ); $wp_query->comments = get_comments( $comment_args ); ?> <ol class="comment-list"> <?php wp_list_comments($wod); ?> </ol>And the wherever
endforeachis, addwp_reset_postdata()right after.There’s some stuff in the middle I’m not sure about either. I don’t think you need any of this:
global $wp_query; $comment_args = array( 'ID' => $post->ID, 'status' => 'approve', 'order' => 'ASC' ); $wp_query->comments = get_comments( $comment_args ); ?>You don’t need any reference to
$wod->ID, and<?php wp_list_comments($wod); ?>can just be<?php wp_list_comments(); ?>.Thanks for the quick reply @jakept ! Something’s not quite right though. Now none of the posts are listing comments. What’s odd to me is that $post->ID is working to show the comment form when comments are open, but wp_list_comments isn’t working.
<?php foreach ( $wods as $post ) : setup_postdata( $post ); ?> <?php if ( comments_open($post->ID) || get_comments_number($post->ID) ) { ?> <?php comment_form(); ?> <ol class="comment-list"> <?php echo wp_list_comments(); ?> </ol>Interesting. Just found get_comments and it seems to be working ok for this situation. It’s a little more manual than wp_list_comments but it should suffice.
$comments = get_comments($post->ID); foreach($comments as $comment) : echo($comment->comment_author); echo($comment->comment_content); endforeach;Ah,
wp_list_comments, works different that I expected.Try:
<?php wp_list_comments( array(), get_comments( array( 'post_id' => $post->ID ) ) ); ?>wp_list_commentsgets the comments for the global$wp_query. Which will only be for the current page. I don’t believeget_postsorWP_Querywill get the comments for each post it finds. So instead you can passget_comments()as the second argument towp_list_comments()to provide your own. In the code up there I added an argument toget_comments()to get comments for the current post in your custom loop.@jakept That last try works perfectly. Thanks so much for the help!
The topic ‘List Comments in get_posts Loop’ is closed to new replies.
(@inhouse)
9 years, 8 months ago
Hello, I’m having trouble trying to list comments in a get_posts loop. The same comments are showing for all posts instead of the relevant comments to each post. I’m not finding much documentation for this specific loop after tons of searches. Hoping I’ve missed something simple. Any help is very appreciated!