
記事を投稿できるアカウント( 主に「投稿者」として )を複数のユーザーに渡しているが、それぞれの投稿数の上限を設定したいって言われた事ありませんか?
そんな時は以下のコードを my-plugin.php に張り付けてみてください。
管理者は「投稿者」「寄稿者」「編集者」のユーザーごとに投稿数の上限を設定して、上限になるとメッセージを表示して「新規投稿」できなくなります。

投稿数にカウントされるのは「公開済み」「下書き」「非公開」「レビュー待ち (保留中)」「ゴミ箱」「予約投稿 」になります。
コード
if ( !class_exists( 'Nendebcom_Posts_Limit_Over_Warning' ) ):
/*
* 投稿数制限
* @package WordPress4.2
*
* License: GPLv2 or later
*/
class Nendebcom_Posts_Limit_Over_Warning {
private $default_posted_upper_limit = 10; //最大投稿数(デフォルト)
public function __construct() {
add_action( 'admin_init', array( $this, 'posts_limit_over_warning' ) );
add_action( 'personal_options_update', array( $this, 'save_post_limit_profile_fields' ) ); //My Profile Update.
add_action( 'edit_user_profile_update', array( $this, 'save_post_limit_profile_fields' ) ); //Other Users Profile Update.
add_action( 'personal_options', array( $this, 'add_post_limit_profile_fields' ) ); //Add Post limit in User Profile Fields.
}
public function posts_limit_over_warning() {
global $wpdb;
global $pagenow;
global $user_ID; //ログインしているユーザーID
global $current_user;
if ( $user_ID && isset( $current_user->roles[ 0 ] ) && ( $current_user->roles[ 0 ] == 'contributor' //contributor 寄稿者
|| $current_user->roles[ 0 ] == 'author' //author 投稿者
|| $current_user->roles[ 0 ] == 'editor' //editor 編集者
)
) {
$sql = "SELECT COUNT(*)";
$sql .= " FROM $wpdb->posts AS P";
$sql .= " WHERE ";
$sql .= " ( P.post_status='publish'"; //公開済み
$sql .= " OR P.post_status='draft'"; //下書き
$sql .= " OR P.post_status='private'"; //非公開
$sql .= " OR P.post_status='pending'"; //レビュー待ち (保留中)
$sql .= " OR P.post_status='trash'"; //ゴミ箱
$sql .= " OR P.post_status='future'"; //予約投稿
//$sql .= " OR P.post_status='attachment'"; //
//$sql .= " OR P.post_status='inherit'"; //継承(添付ファイル、改訂履歴・自動保存のとき)
$sql .= " )";
$sql .= " AND P.post_type ='post' ";
$sql .= $wpdb->prepare( ' AND P.post_author = %d', $user_ID );
$post_count = $wpdb->get_var( $sql );
//ユーザーの最大投稿数
$posted_upper_limit = get_user_meta( $user_ID, 'user_post_limit', true );
if ( $posted_upper_limit == '' ) {
$posted_upper_limit = $this->default_posted_upper_limit;
}
if ( $posted_upper_limit != 0 && $post_count >= $posted_upper_limit ) {
//物件新規登録ページを非表示
if ( !isset( $_GET[ 'post_type' ] ) && $pagenow == 'post-new.php' ) {
wp_die( '投稿の新規登録はできません。' );
}
//管理サブメニューから投稿の「新規追加」を削除
global $submenu;
unset( $submenu[ 'edit.php' ][ 10 ] ); // 新規追加
//お知らせ 投稿一覧と投稿編集ページで表示
if ( $pagenow == 'edit.php' || $pagenow == 'post.php' ) {
add_action( 'admin_notices', array( $this, 'post_over_notices' ) );
}
//ヘッダの「+新規」ドロップダウンメニューから[投稿]を削除
add_action( 'admin_bar_menu', array( $this, 'remove_admin_bar_menu' ), 202 );
//投稿一覧ページで h1タイトル 横の[新規追加]ボタンを非表示 CSS
add_action( 'admin_print_styles-edit.php', array( $this, 'edit_post_page_style' ), 90 );
//投稿編集ページで h1タイトル 横の「物件登録」ボタンを非表示 CSS
add_action( 'admin_print_styles-post.php', array( $this, 'edit_post_page_style' ) );
//remove a button in the post/page edit screen to create a clone
remove_action( 'post_submitbox_start', 'duplicate_post_add_duplicate_post_button' );
//remove the link to action list for post_row_actions
remove_filter( 'post_row_actions', 'duplicate_post_make_duplicate_link_row', 10, 2 );
}
}
}
/**
* Save Post limit in User Profile Fields.
*
* @param int $user_id
*/
public function save_post_limit_profile_fields( $user_id ) {
global $current_user;
if ( $current_user->roles[ 0 ] == 'administrator' ) { //administrator 管理者
$num = isset( $_POST[ 'user_post_limit' ] ) ? $_POST[ 'user_post_limit' ] : '';
if ( !preg_match( "/^[0-9]+$/", $num ) ) {
$num = '';
}
update_user_meta( $user_id, 'user_post_limit', $num, get_user_meta( $user_id, 'user_post_limit', true ) );
}
}
/**
* Add Post limit in User Profile Fields.
*
* @param array $profileuser
*/
public function add_post_limit_profile_fields( $profileuser ) {
global $current_user;
if ( $current_user->roles[ 0 ] == 'administrator' ) { //administrator 管理者
echo '<tr>';
echo '<th scope="row">投稿数制限</th>';
echo '<td>最大投稿数 <input type="number" name="user_post_limit" size="4" value="' . get_user_meta( $profileuser->ID, 'user_post_limit', true ) . '" />件 ( 空欄: デフォルト値 0:無制限。) </td>';
echo '</tr>';
}
}
/**
* お知らせ
*/
public function post_over_notices() {
if ( in_array( $GLOBALS[ 'current_screen' ]->post_type, array( 'post' ) ) ) {
echo '<div class="error" style="text-align: center;"><p>設定の投稿数に達しました。 投稿の新規登録はできません。</p></div>';
}
}
/**
* ヘッダの「+新規」ドロップダウンメニューから[投稿]を削除
*/
public function remove_admin_bar_menu( $wp_admin_bar ) {
$wp_admin_bar->remove_menu( 'new-post' );
}
/**
* 投稿一覧/投稿編集ページで h1タイトル 横の[新規追加]ボタンを非表示 CSS
*/
public function edit_post_page_style() {
if ( in_array( $GLOBALS[ 'current_screen' ]->post_type, array( 'post' ) ) ) {
?>
<style type="text/css">
#wpbody .wrap h1 > a.page-title-action{ display:none; }
</style>
<?php
}
}
}
if ( is_admin() ) {
new Nendebcom_Posts_Limit_Over_Warning();
}
endif;
初期値
以下の値を利用する環境にあわせて変更してください。
最大投稿数(デフォルト)
private $default_posted_upper_limit = 10; //最大投稿数(デフォルト)
各ユーザーごとの最大投稿数を設定をしていない場合は ここのデフォルト値になります。
スポンサードリンク
画面説明
ユーザープロフィール画面
通常はデフォルト値ですが、ユーザーごとに投稿数の上限を変更する事もできます。
各ユーザーのプロフィール画面では 最大投稿数の設定は 管理者だけが表示され、変更できます。

投稿一覧
各ユーザーは投稿数の上限に達すると ヘッダーにメッセージが表示されて「新規追加」のボタンがなくなります。


投稿編集
投稿数の上限に達すると ヘッダーにメッセージが表示されて「新規追加」のボタンがなくなります。


メニュー
投稿数の上限に達するとサブメニューの「新規追加」のリンクと ヘッダー「+新規」のプルダウン部分の「投稿」リンクがなくなります。

新規投稿
新規投稿が制限されると新規投稿のページへ行っても投稿することはできません。

※今回は投稿数制限する部分だけ説明しました。
参考
WordPress Codex ユーザーの種類と権限
http://wpdocs.osdn.jp/ユーザーの種類と権限
WordPress Codex Plugin API/Action Reference/admin init
http://codex.wordpress.org/Plugin_API/Action_Reference/admin_init
WordPress Codex Plugin API/Action Reference/personal options update
https://codex.wordpress.org/Plugin_API/Action_Reference/personal_options_update
WordPress Codex Plugin API/Action Reference/edit user profile update
https://codex.wordpress.org/Plugin_API/Action_Reference/edit_user_profile_update
WordPress Codex Plugin API/Action Reference/personal options
https://codex.wordpress.org/Plugin_API/Action_Reference/personal_options
WordPress Codex Plugin API/Action Reference/admin notices
https://codex.wordpress.org/Plugin_API/Action_Reference/admin_notices
WordPress Code Reference do_action ( “admin_print_styles-{$hook_suffix}” )
https://developer.wordpress.org/reference/hooks/admin_print_styles-hook_suffix/