You will need to interrupt the post submission process much sooner. Your code hooked to admin_init
stands a pretty good chance of working, with a modification or two, and wp_die
is pretty harsh. I’d just redirect back to the originating page without saving, if it were me.
Something like (untested)…
function check_post_limit() {
if (!isset($_POST['ID'])) return;
global $wpdb;
if( $userdata->ID != 1) {
$post_count = $wpdb->get_var( "SELECT count(*) FROM $wpdb->posts WHERE DATE(post_modified) = DATE(NOW()) AND post_author = $userdata->ID" );
}
if( isset($post_count) && $post_count > 3 ) {
wp_safe_redirect(admin_url('post.php?post=".$_POST["post_ID'].'&action=edit'));
exit;
}
}
A couple of notes:
- Your code as written would have thrown a notice for the user with ID
== 1 because$post_count
would not be set. - Checking for user ID == 1 is questionable. Suppose your admin (which
is what I assume you want) has some other ID (and for security it is
probably a good idea to have your admin have some other ID). I’d
write the check based on user role instead.