I wrote a function to limit 1 post by user in each custom post types (I created 3 custom post types).
The problem is that my function limit 1 post for ALL users and not 1 post for each user.
Can I have some help please ? What can I add ?
$post_types_name = array('subject-imposed', 'subject-free', 'dissertation');
function limit_posts() {
global $post_types_name;
global $pagenow;
foreach ($post_types_name as $post_type_name) {
$count_post = wp_count_posts($post_type_name)->publish;
$max_posts = 1; //the number of max published posts
if($count_post >= $max_posts){
if (! empty($pagenow) && ('post-new.php' === $pagenow && ( $_GET['post_type'] == $post_type_name)) || ! empty($pagenow) && ('edit.php' === $pagenow && ( $_GET['post_type'] == $post_type_name)) ){
?>
<style>
.editor-limit {
text-align: center;
}
</style>
<script type="text/javascript">
jQuery(window).load(function() {
jQuery('.editor-styles-wrapper').html('<p class="editor-limit">You have reached the maximum number of posts.</p>');
});
</script>
<?php
}
}
}
}
add_action('admin_head' , 'limit_posts');
I wrote a function to limit 1 post by user in each custom post types (I created 3 custom post types).
The problem is that my function limit 1 post for ALL users and not 1 post for each user.
Can I have some help please ? What can I add ?
$post_types_name = array('subject-imposed', 'subject-free', 'dissertation');
function limit_posts() {
global $post_types_name;
global $pagenow;
foreach ($post_types_name as $post_type_name) {
$count_post = wp_count_posts($post_type_name)->publish;
$max_posts = 1; //the number of max published posts
if($count_post >= $max_posts){
if (! empty($pagenow) && ('post-new.php' === $pagenow && ( $_GET['post_type'] == $post_type_name)) || ! empty($pagenow) && ('edit.php' === $pagenow && ( $_GET['post_type'] == $post_type_name)) ){
?>
<style>
.editor-limit {
text-align: center;
}
</style>
<script type="text/javascript">
jQuery(window).load(function() {
jQuery('.editor-styles-wrapper').html('<p class="editor-limit">You have reached the maximum number of posts.</p>');
});
</script>
<?php
}
}
}
}
add_action('admin_head' , 'limit_posts');
Share
Improve this question
asked Jun 4, 2020 at 1:36
JandonJandon
1471 silver badge9 bronze badges
1 Answer
Reset to default 0You’re using wp_count_posts(
for counting posts, so it will return the number of posts.
If you want to get the number of posts for given user, you should use count_user_posts
instead.
So change this line:
$count_post = wp_count_posts($post_type_name)->publish;
to:
$count_post = count_user_posts( get_current_user_id(), $post_type_name, true );