【发布时间】:2021-12-29 13:09:26
【问题描述】:
当有人点击Add New帖子时,会出现一条消息,例如“您不允许创建帖子”,并且用户也不允许发布或起草帖子。
我试过这段代码-:
function post_published_limit( $ID, $post ) {
$max_posts = 5; // change this or set it as an option that you can retrieve.
$author = $post->post_author; // Post author ID.
$count = count_user_posts( $author, 'post'); // get author post count
if ( $count > $max_posts ) {
// count too high, let's set it to draft.
$post->post_status = 'draft';
wp_update_post( $post);
}
}
add_action( 'publish_post', 'post_published_limit', 10, 2 );
此代码帮助我不发布帖子,但它会创建该帖子的草稿。
【问题讨论】:
-
你对这段代码有什么问题/疑问?
-
我什至不想草稿。
-
那么不要包含
wp_update_post( $post); -
$post->post_status = 'draft';显然是发生这种情况的地方。据我了解,publish_post钩子在实际提交帖子时运行。当用户已经经历了编写它的麻烦时,您不想拒绝它。而是找到当用户尝试创建帖子时执行的action hook。 -
@MarkusAO 正确
标签: php wordpress post hook action