Show all comments for all languages
-
Hi,
I see this has been a problem for many reding different threads and I tried what Chouby gave as a filer in one of those, which is:
function polylang_remove_comments_filter() { global $polylang; remove_filter('comments_clauses', array(&$polylang->filters, 'comments_clauses')); } add_action('wp','polylang_remove_comments_filter');
but it didn’t work and reading other posts with code in it I was able to get something that works.
My hope now is that someone validate this if everything is fine, if it can be optimized and maybe if Chouby is reading that he can simply add this as an option that would make it fully supported by the plugin.
/*** Show all comments for all languages ***/ //Remove the filter function polylang_remove_comments_filter() { global $polylang; remove_filter('comments_clauses', array(&$polylang, 'comments_clauses')); } function sort_merged_comments($a, $b) { return $a->comment_ID - $b->comment_ID; } //Get the comments from the same post in different languages function merge_comments($comments, $post_ID) { global $sitepress; remove_filter( 'comments_clauses', array( $sitepress, 'comments_clauses' ) ); // get all the languages for which this post exists $languages = icl_get_languages(); $post = get_post( $post_ID ); $type = $post->post_type; foreach($languages as $code => $l) { // in $comments are already the comments from the current language if(!$l['active']) { $otherID = icl_object_id($post_ID, $type, false, $l['language_code']); $othercomments = get_comments( array('post_id' => $otherID, 'status' => 'approve', 'order' => 'ASC') ); $comments = array_merge($comments, $othercomments); } } if ($languages) { // if we merged some comments in we need to reestablish an order usort($comments, 'sort_merged_comments'); } // add_filter( 'comments_clauses', array( $sitepress, 'comments_clauses' ) ); return $comments; } //Add comments for the same posts in different languages function merge_comment_count($count, $post_ID) { // get all the languages for which this post exists $languages = icl_get_languages(); $post = get_post( $post_ID ); $type = $post->post_type; foreach($languages as $l) { // in $count is already the count from the current language if(!$l['active']) { $otherID = icl_object_id($post_ID, $type, false, $l['language_code']); if($otherID) { // cannot use call_user_func due to php regressions if ($type == 'page') { $otherpost = get_page($otherID); } else { $otherpost = get_post($otherID); } if ($otherpost) { // increment comment count using translation post comment count. $count = $count + $otherpost->comment_count; } } } } return $count; } //Trigger the functions add_action('wp','polylang_remove_comments_filter'); add_filter('comments_array', 'merge_comments', 100, 2); add_filter('get_comments_number', 'merge_comment_count', 100, 2);
This was tested with posts only but I see pages should be supported.
- The topic ‘Show all comments for all languages’ is closed to new replies.