投稿数を制限する(カスタムポストタイプ対応)

1
2
3
4
5
6
7
8
9
function posts_limit() {
    global $userdata, $post_type, $wpdb;
    if ( $post_type === 'ポストタイプ' ) {
        $item_count = $wpdb->get_var( "SELECT count(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'ポストタイプ' AND post_author = $userdata->ID" );
        if ( $item_count >= '制限数' ) { wp_die( "エラーメッセージ" ); }
    }
    return;
}
add_action('admin_head-post-new.php', 'posts_limit');

functions.php 未検証