这个\'post_id\'
在中转换为正整数WP_Comment_Query
, 因此,您无法成功地将任何其他内容传递给get_comments()
.
你必须过滤\'comments_clauses\'
. 在这里,您可以更改WHERE
要使用的子句comment_post_ID IN ( $ids )
而不是comment_post_ID = $id
.
我将使用静态类作为get_comments()
.
示例代码
/**
* Query comments for multiple post IDs.
*/
class T5_Multipost_Comments
{
/**
* Post IDs, eg. array ( 1, 2, 40 )
* @var array
*/
protected static $post_ids = array ();
/**
* Called like get_comments.
*
* @param array $args
* @param array $post_ids
* @return array
*/
public static function get( $args = array (), $post_ids = array () )
{
if ( array () !== $post_ids )
{
self::$post_ids = $post_ids;
add_filter( \'comments_clauses\', array ( __CLASS__, \'filter_where_clause\' ) );
}
return get_comments( $args );
}
/**
* Filter the comment query
*
* @param array $q Query parts, see WP_Comment_Query::query()
*
* @return array
*/
public static function filter_where_clause( $q )
{
$ids = implode( \', \', self::$post_ids );
$_where_in = " AND comment_post_ID IN ( $ids )";
if ( FALSE !== strpos( $q[\'where\'], \' AND comment_post_ID =\' ) )
{
$q[\'where\'] = preg_replace(
\'~ AND comment_post_ID = \\d+~\',
$_where_in,
$q[\'where\']
);
}
else
{
$q[\'where\'] .= $_where_in;
}
remove_filter( \'comments_clauses\', array ( __CLASS__, \'filter_where_clause\' ) );
return $q;
}
}
用法示例
$multi_comments = T5_Multipost_Comments::get(
array ( \'number\' => \'14\' ), // comment args
array ( 149, 564, 151 ) // post IDs
);
print \'<pre>\' . htmlspecialchars( print_r( $multi_comments, TRUE ) ) . \'</pre>\';