限制每天和每个用户的评论已内置到当前查询中。唯一缺少的是检查帖子ID。这里有一个非常方便的函数/模板标记:
/**
* @param int $limit
* @param int $time
* @param int $ids
*/
function is_user_limit_reached( $limit = 5, $time = 1, $ids = array() )
{
global $wpdb;
// Cast to array
! is_array( $limited ) AND $limited = array( $limited );
// Generate SQL clause
$ids = ! empty( $ids )
? sprintf( " and comment_post_ID in (%s)", join( ",", $ids ) )
: "";
// Rows: user_id, comment_date, comment_post_ID
$count = $wpdb->query( $wpdb->prepare(
"select count(*)
from {$wpdb->comments}
where user_id=%d
and comment_date >= date_sub( now(), interval %d day )
%s",
get_current_user_id(),
$time,
$ids
) );
return $count >= $limit;
}
您可以这样使用它:
if ( is_user_limit_reached(
10, // allowed comments per day
7, // in the last 7 days
array( 1, 35, 97, 1148 ) // array of post IDs which have a limit
) )
{
echo \'Your limit is already reached. Come back tomorrow\';
}
else
{
comment_form();
}
你也可以打开/关闭评论框模板标签,等等。天空是你的极限。