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.
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 aforeach
loop.