wordpress作者帖子数量限制

wordpress作者帖子数量限制,wordpress,Wordpress,我正试图限制作者的帖子,目前我允许2篇帖子不超过2篇,目前我有几个用户的帖子超过2篇,所以我如何修复我的代码,使其不允许他们发表更多的帖子 if ($post_count < 2) { return TRUE; } else { echo 'You have already posted your maximum number of posts.'; return FALSE; } } if($post\u count

我正试图限制作者的帖子,目前我允许2篇帖子不超过2篇,目前我有几个用户的帖子超过2篇,所以我如何修复我的代码,使其不允许他们发表更多的帖子

 if ($post_count < 2) {
        return TRUE;
    } else {
       echo 'You have already posted your maximum number of posts.';
        return FALSE;
    }
}
if($post\u count<2){
返回TRUE;
}否则{
echo“您已经发布了最多数量的帖子。”;
返回FALSE;
}
}

正如我所说,很少有作者有超过2篇文章,因此使用此代码将允许他们发表文章,因为if语句将被绕过,因为它试图检测值2,因为他们已经超过了该值。

您应该使用wp\u insert\u post\u数据过滤器。在这个过滤器中定义你的条件。 所以会是这样的:

add_filter('wp_insert_post_data', 'aa_func_20150916080916', 20, 2);
function aa_func_20150916080916($data , $postarr)
{
  $warn = "You have already posted your maximum number of posts";
  $current_user = get_current_user_id();
  $user_post_count = count_user_posts($current_user);
  if($user_post_count >2 ) {
    return wp_die($warn);
  }
  return $data;
}
请注意:这是一个快速回答,您应该根据自己的目的更改代码

文件: