[resolved] [Plugin : Polylang] - How to get the latest comments for the current language ? (4 posts)

  1. ben.IT
    Posted 3 years ago #

    My current theme lists the last comments on the home page.
    I wonder how can I do to query only the comments in the page language ?

    Actually, The theme uses this query :
    $comments = get_comments( array( 'number' => $number, 'status' => 'approve' ) );

    I would like to the same but adding the language criteria, any idea ?
    greetings, ben


  2. ben.IT
    Posted 3 years ago #

    Hi, I find a solution which works using the fine polylang API :

    //Original : does not manage language. display all comments. doesn't care about the comment language
    		//$comments = get_comments( array( 'number' => $number, 'status' => 'approve' ) );
    		$current_lng= pll_current_language(); //retrieve current language
    		$lng_objects = get_posts(array(
    		'post_type' => 'post',
    		'lang' => $current_lng, // use language slug in the query
    		'showposts' => 150 //remove if problem. here for perf. this prevents loading all posts.
    $lng_ids=array() ;
    foreach ($lng_objects as $objects)
    	array_push($lng_ids, $objects->ID) ;
    $string_lng_ids=implode(",", $lng_ids); //build ID string for sql query
    global $wpdb;
    select wp3_comments.*
    from wp3_comments
    inner join wp3_posts on wp3_posts.ID=wp3_comments.comment_post_ID
    where comment_approved=1
    and wp3_posts.ID in ($string_lng_ids)
    order by comment_date desc
    limit 0,$number

    Hope this could help.

    [Moderator Note: Please post code or markup snippets between backticks or use the code button. As it stands, your code may now have been permanently damaged/corrupted by the forum's parser.]

  3. Chouby
    Plugin Author

    Posted 3 years ago #

    Thanks for sharing! However, I am surprised that it does not work for you as it currently works for the widget "recent comments" which uses the same function get_comments

  4. ben.IT
    Posted 3 years ago #

    Don't know why either. Thanks for your answer.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Polylang
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic