Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Tirips

    (@tirips)

    No matter for highlight as we can selected posts by language.
    But PLEASE help me for the comments.
    I can’t find where I have to put your solution to remove comment filter.
    Waiting your answer….
    Thanks

    Let me sum up your need :
    You want to display all the comments for one post whatever their language (ie merge the comments made for the same post in different languages).

    This can be achieved in 3 steps :
    1. Remove the filter on comments that loads only those in the current language
    2. Modify the function that built the list of comments (comments_array)
    3. Modify the function that count the comments (get_comments_number)

    Theses 3 steps can be programmed in your functions.php
    You’ll find here after the total code to be included in your functions.php
    It is the consolidation of the code written at http://wordpress.org/support/topic/multilang-comment (that use modified fucntions of the WPML comment merging plugin.
    Note that the functions that modify comments_array and get_comments_number are using WPML functions (icl_…) instead of Polylang ones (pll_…) as polylang contains a “compatibility” module (polylang/include/wpml-compat.php) that allow us to use WPML functions.

    Here is the code to put in functions.php :

    //Remove the filter
    function polylang_remove_comments_filter() {
        global $wp_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 posts 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('skip_missing=1');
    	$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('skip_missing=1');
    	$languages = pll_the_languages(array('raw'=>1 ));
    	$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);

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

    Tell us if it is working as you want.
    If yes, do not forget to tag this thread as [Resolved]

    Thread Starter Tirips

    (@tirips)

    Iznogood is very good !!
    Thanks a lot for your help, that works !

    It’s not working for me. I am using it for a page, not for posts. Does that make a difference?

    I see the right number displayed of the merged comments (2324 comments) on both pages.
    The comments themselves are not merged however:
    One page shows 2323 comments.
    The other page shows just one 1 test comment.

    @rodneyyates: If you require assistance then, as per the Forum Welcome, please post your own topic. This topic references an older version of WordPress.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Multilang comments hightlight post’ is closed to new replies.