Чтобы получить все комментарии, связанные с определенной публикацией в WordPress, вы можете использовать различные методы. Вот несколько примеров кода:
Метод 1: использование WP_Query
$args = array(
'post_type' => 'post',
'post_id' => 123, // Replace 123 with the ID of your post
'status' => 'approve', // Optional: Retrieve only approved comments
);
$comments_query = new WP_Query( $args );
if ( $comments_query->have_comments() ) {
while ( $comments_query->have_comments() ) {
$comments_query->the_comment();
// Display or process the comment data here
echo get_comment_text();
}
} else {
echo 'No comments found.';
}
Метод 2: использование get_comments()
$args = array(
'post_id' => 123, // Replace 123 with the ID of your post
'status' => 'approve', // Optional: Retrieve only approved comments
);
$comments = get_comments( $args );
foreach ( $comments as $comment ) {
// Display or process the comment data here
echo $comment->comment_content;
}
Метод 3. Использование специального SQL-запроса
global $wpdb;
$post_id = 123; // Replace 123 with the ID of your post
$comments = $wpdb->get_results(
"SELECT * FROM $wpdb->comments
WHERE comment_post_ID = $post_id
AND comment_approved = '1'"
);
foreach ( $comments as $comment ) {
// Display or process the comment data here
echo $comment->comment_content;
}
Эти методы позволяют получить все комментарии, связанные с определенной публикацией в WordPress. Вы можете выбрать метод, который лучше всего соответствует вашим потребностям.