admin管理员组文章数量:1335665
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 );
本文标签: Limit 1 post by user in each custom post type
版权声明:本文标题:Limit 1 post by user in each custom post type 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742376416a2463253.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论