admin管理员组

文章数量:1323335

I try to get all comments of a specific post, but it is not working like the way I do want. I use $assignment->ID to get all comments for a specific page ID. Even when I change $assignment->ID to 101 it is not working and he still shows all the comments of all posts.

foreach($assignments as $assignment) {

    echo $assignment->post_title;
                                
    $args = array(
        'number' => 0,
        'status' => 'approve',
        // shows all comments, but it shouldn't
        'comment_post_ID' => $assignment->ID
    );
                                
    $comments = get_comments( $args );
                                
    if ( $comments ) {
        foreach ( $comments as $comment )   {
            echo '<li>';
            echo $comment->comment_content;
            echo '</li>';
        }
    }

}

I think I miss an important $arg, but I'm not sure which one. It doesn't matter what I do, all comments are showing up everytime..

I try to get all comments of a specific post, but it is not working like the way I do want. I use $assignment->ID to get all comments for a specific page ID. Even when I change $assignment->ID to 101 it is not working and he still shows all the comments of all posts.

foreach($assignments as $assignment) {

    echo $assignment->post_title;
                                
    $args = array(
        'number' => 0,
        'status' => 'approve',
        // shows all comments, but it shouldn't
        'comment_post_ID' => $assignment->ID
    );
                                
    $comments = get_comments( $args );
                                
    if ( $comments ) {
        foreach ( $comments as $comment )   {
            echo '<li>';
            echo $comment->comment_content;
            echo '</li>';
        }
    }

}

I think I miss an important $arg, but I'm not sure which one. It doesn't matter what I do, all comments are showing up everytime..

Share Improve this question asked Sep 6, 2020 at 19:31 TetragrammatonTetragrammaton 191 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Based on developer documents of get_comments functions, it is using args like WP_Comment_Query::__construct Method which is accepts post_id in the argument's array. So, the code will be like:

foreach($assignments as $assignment) {

    echo $assignment->post_title;
                                
    $args = array(
        'number' => 0,
        'status' => 'approve',
        // shows all comments, but it shouldn't
        'post_id' => $assignment->ID
    );
                                
    $comments = get_comments( $args );
                                
    if ( $comments ) {
        foreach ( $comments as $comment )   {
            echo '<li>';
            echo $comment->comment_content;
            echo '</li>';
        }
    }

}

Links:

https://developer.wordpress/reference/functions/get_comments/ https://developer.wordpress/reference/classes/wp_comment_query/__construct/

本文标签: Get all comments associated with a specific page ID (commentpostID)