• Resolved InHouse

    (@inhouse)


    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!

    $args = array (
    	'post_type' => array( 'wod' ),
    	'order' => 'ASC',
    	'orderby' => 'menu_order',
    	'post_per_page' => 999
    );
    $wods = get_posts( $args ); ?>
    
    <?php foreach ( $wods as $wod ) : setup_postdata( $wod );
    	if ( comments_open($wod->ID) || get_comments_number($wod->ID) ) {
    		comment_form();
    		global $wod, $wp_query;
    	    $comment_args = array(
    	        'ID'       => $wod->ID,
    	        'status' => 'approve',
    	        'order'   => 'ASC'
    	    );
    	    $wp_query->comments = get_comments( $comment_args ); ?>
    		    <ol class="comment-list">
    			    <?php wp_list_comments($wod); ?>
    		    </ol>
Viewing 6 replies - 1 through 6 (of 6 total)
  • Ah, this old chestnut. The issue is with setup_postdata( $wod );. When using setup_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 endforeach is, add wp_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(); ?>.

    Thread Starter InHouse

    (@inhouse)

    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>
    Thread Starter InHouse

    (@inhouse)

    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_comments gets the comments for the global $wp_query. Which will only be for the current page. I don’t believe get_posts or WP_Query will get the comments for each post it finds. So instead you can pass get_comments() as the second argument to wp_list_comments() to provide your own. In the code up there I added an argument to get_comments() to get comments for the current post in your custom loop.

    Thread Starter InHouse

    (@inhouse)

    @jakept That last try works perfectly. Thanks so much for the help!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘List Comments in get_posts Loop’ is closed to new replies.