1

I am trying to get comments from another post. I am getting the post_id and doing a check to see if it has comments

I can get the comments for the current post every time but for the other post I am getting mixed results (I did get it working but am unable to reproduce the results)

This is the code I'm working on with some alternate attempts commented out:

if(get_comments_number($getLangsPostID) > 0){
    //echo "<pre>This post id is ".$thisPostID." The other language post id is (".$getLangsPostID."):";
    //var_dump($getLangsPostID);
    $altLangReq = array(
        'post_id' => $getLangsPostID // have tried with and without trailing comma
    );

    //$langComments = get_comments($altLangReq);
    $langComments = get_comments('post_id='.$getLangsPostID);

    echo "<p>Current post ($thisPostID), request for post id (".$getLangsPostID.") is:<pre>";
    var_dump($langComments);
    //$x = get_comments('post_id=31'); // hard coded also fails
    $x = get_comments(array('post_id'=>31));
    var_dump($x);
    echo "</pre></p>";

If it matters I am using polylang and trying to show comments from all language alternatives on each languages post. I am using two posts (id 21 and 31) which have 3 or 4 comments each (these are getting found by get_comments_number.

Also I am outside the comment loop on the comments.php template in slightly customised underscore theme.

Edit:

Some clarification, I wanted to describe most of the conditions incase there was some other factor but basically if I call:

var_dump(get_comments('post_id=21')); // 21 is the current post

I get the current posts comments, if I call:

var_dump(get_comments('post_id=31')); // 31 is a different post

I get an empty array, if I go to post 31, then 31 works and 21 returns an empty array but this seems to be the way to call posts other than the current one (via the post_id). See the first example in the WP codex

Edit 2 Answer: (I am adding this for now as I am not able to answer my own question for 8 hours ATM)

Turns out it was the language plugin... When I call the comments it adds a language filter:

'lang' => $currentLang

So I had to explicitly override that:

get_comments(array('post_id'=>31, 'lang' => $targetLang));

I still don't know why I was sometimes able to get the other comments without this.

  • Set debug to true in wp-config.php. It seems like a lot of undefined variables – Pieter Goosen Jun 10 '14 at 4:17
  • @PieterGoosen Thanks for the advice, I have debug on, and the debug tools installed, all variables are defined and working ($getLangsPostID = 31) I have only copied the inner loop of my tests. The var_dump's are returning empty arrays for the not current post and the list of comments for the current post. – Totoro Jun 10 '14 at 5:05
  • You should have to go back to get_comments again. $post_id doesn't accept an array or a string, just a single integer. You'll have to somewhere break things up into a foreach loop. – Pieter Goosen Jun 10 '14 at 8:17
  • @PieterGoosen if I enter get_comments(31) on post 21 it returns the comments for 21 - I assume because it is invalid and using the default behaviour. If you look at the examples in the codex it can take a GET formatted string or array. – Totoro Jun 10 '14 at 8:24
1

Turns out it was my use of the language plugin (polylang)... When I call the comments it adds a language filter:

'lang' => $currentLang

So I had to explicitly override that:

get_comments(array('post_id'=>31, 'lang' => $targetLang));

I still don't know why I was sometimes able to get the other comments without this.

-2

The post must be have a comment.

$comments = get_comments( array( 'post_id' => $id ) );

if( empty( $comments ) ) {
    return;
}

foreach( $comments as $val ) { 
   echo $val->comment_content;
}
  • 1
    The OP does state that they have comments on the posts that they are trying to access. Please expand your answer and demonstrate how it solves some or all of the issues in the original question. – Andy Macaulay-Brook Aug 22 '16 at 14:03
  • Thanks for the answer. As you can see I already answered my own question 2 years ago (It was the plug in) I was looking to put different language comments on all the posts (So if you have an English and French post, you can see the English AND French comments but the plugin was hiding comments in other languages. – Totoro Aug 23 '16 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.