如何仅在当前登录用户的帖子上获得评论?

时间:2014-10-23 作者:Maarten Heideman

我需要一个查询,只从当前登录的用户是作者的帖子中获取所有评论。

每个用户(作者)都可以发布新帖子。然后我需要显示他们自己的帖子列表(已经可以了)。现在我想要一个所有评论的列表(在当前登录的用户帖子上)。因此,作者对他们帖子上的所有评论都有一个快速的概述。

2 个回复
最合适的回答,由SO网友:Bordoni 整理而成

使用@shanebp的思想get_comments:

$args = array(
    \'post_author\' => get_current_user_id(), // It will use the current logged in use to grab the comments
);
$comments = get_comments( $args );

// Referer to http://codex.wordpress.org/Function_Reference/wp_list_comments
$list_args = array(
    \'reverse_top_level\' => false // Show the latest comments at the top of the list
);
wp_list_comments( $list_args, $comments );
这将显示一个用户对帖子的评论列表。

SO网友:kaiser

实际上WP_Comment_Query 类几乎支持您想要的所有内容。您可能想使用post_author 获取评论or user_id. 两者的作用几乎相同。如您所见post_statuspublish 仅适用于岗位类型post. 根据需要更改参数。

if ( ! is_user_logged_in() )
    return print \'Nothing to see here\';

$comment_query = new WP_Comment_Query;
$comments = $comment_query->query( array (
    \'status\'         => \'approve\',
    \'type\'           => \'comment\',
    // \'post_author\'    => get_current_user_id(),
    \'post_status\'    => \'publish\',
    \'post_type\'      => \'post\',
    \'user_id\'        => get_current_user_id(),
    \'number\'         => 20,
    \'offset\'         => 0,
    \'order\'          => \'DESC\',
    \'orderby\'        => \'comment_date\',
    \'count\'          => true,
) );
foreach ( $comments as $comment )
    var_dump( $comment );

结束

相关推荐

comments in Admin

我对管理员中帖子的评论部分实际上做了什么感到困惑。我在帖子的屏幕选项中打开了评论。当我显示帖子时,我会看到一个评论框。如果我在评论框中输入了什么,它会发生什么?当我在管理中单击“显示评论”时,我从未看到它。但在某些主题上,它会出现在帖子中。