检查最近是否有人在我的帖子中发表了评论

时间:2018-03-22 作者:Jamille

是否有类似于-

If someone recently commented in one of my posts (not other author post), I want his/her ID?

如果没有这样的功能,我如何实现它?

实际上,我正在建立一个通知系统,它将是这样的

UserName 对您的postTitle.

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

看来你得和current user.

因为这是一个通知系统,你必须post Ids 按当前用户并全局检查。

否则,你必须检查每一个帖子。您将无法从网站的任何位置访问这些值。

这是获取最新(注释,注释用户id)的代码。。。全局:)

// Grab all posts\' (array)ids by current user
function current_author_post_ids() {

    global $current_user;

    $args = array(
        \'author\' => $current_user->ID,
        \'post_type\' => \'post\',
        \'post_status\' => \'publish\',
        \'posts_per_page\' => -1
    ); // get all posts


    $author_posts = get_posts($args);

    // check if only one id return
    if (count($author_posts) > 1) {

        $allIds = array();

        foreach ($author_posts as $c_post) :
            $allIds[] = $c_post->ID;
        endforeach;

        return $allIds;

    } else {

        return $author_posts[0]->ID;

    }

}

function get_last_comment_and_author_id() {
    $args = array(
        \'number\' => \'1\',
        \'post__in\' => current_author_post_ids(),
        \'orderby\' => \'post_date\',

        // order by Time Stamp Here
        // \'oderby\' => \'meta_type\' TIME

        \'order\' => \'DSC\',
        \'posts_per_page\' => 1
    ); // get only one comment


    // Get the latest comment

    $comments = get_comments($args);

    foreach ($comments as $comment) :
        echo \'Last author ID =\' . $comment->user_id . \'<br>\';
        echo \'Last author =\' . $comment->comment_author . \'<br>\';
        echo \'Last Comment =\' . $comment->comment_content;
    endforeach;

}
要获取值,可以运行此函数

 <?php echo get_last_comment_and_author_id(); ?>

SO网友:mmm

要检索帖子的最后一条评论,可以尝试以下代码:

$comments = get_comments([
    "post_id" => $post_id,
    "number" => 1,
]);

if (isset($comments[0])) {

    $lastComment = $comments[0];

    if ("0" === $lastComment->user_id) {
        // comment of a non connected user
    } else {
        // author identifier is in $lastComment->user_id
    }
}

结束