Description
This is the code for render_block_core_comment_template() in block_library/comment_template/index.php:
// Get an array of comments for the current post.
$comments = $comment_query->get_comments();
if ( count( $comments ) === 0 ) {
return '';
}
$comment_order = get_option( 'comment_order' );
if ( 'desc' === $comment_order ) {
$comments = array_reverse( $comments );
}
$wrapper_attributes = get_block_wrapper_attributes();
return sprintf(
'<ol %1$s>%2$s</ol>',
$wrapper_attributes,
block_core_comment_template_render_comments( $comments, $block )
);
That doesn't allow a plugin author to provide a custom ordering. In my case I am adding comment voting and we want to reorder comments by their up/down votes. The following line should be inserted prior to the $wrapper_attributes assignment:
$comments = apply_filters( 'comments_array', $comments, $block->context['postId'] );
Step-by-step reproduction instructions
- In any blocks-based theme create a plugin with a comments ordering filter using
add_filter( 'comments_array', function ( $comments, $post_id ) { return array_reverse( $comments ); }, 10, 2 );
- Create a post and add two or more comments
- Load the post. Expected result: is comments in reverse order; Actual result: comments in original order
Screenshots, screen recording, code snippet
No response
Environment info
Wordpress version: 6.1.1, Gutenberg version: Unknown but it matches git, Theme: Twenty TwentyThree
Browser: Chrome, but not pertinent
Device: Host is linux, viewed on linux, Windows, and Android
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes
Description
This is the code for
render_block_core_comment_template()in block_library/comment_template/index.php:That doesn't allow a plugin author to provide a custom ordering. In my case I am adding comment voting and we want to reorder comments by their up/down votes. The following line should be inserted prior to the
$wrapper_attributesassignment:Step-by-step reproduction instructions
add_filter( 'comments_array', function ( $comments, $post_id ) { return array_reverse( $comments ); }, 10, 2 );Screenshots, screen recording, code snippet
No response
Environment info
Wordpress version: 6.1.1, Gutenberg version: Unknown but it matches git, Theme: Twenty TwentyThree
Browser: Chrome, but not pertinent
Device: Host is linux, viewed on linux, Windows, and Android
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes