如何解决如何按类别限制帖子
我想知道是否有办法按类别限制此代码。换句话说,如果用户 A 已经在 A 类中发布了 10 个帖子,那么他可以将新的帖子保留在 A 类中作为草稿或在 B 类中再发布 10 个。
* Limit author creation of new posts
*
* @param array $data An array of slashed,sanitized,and processed post data.
* @param array $postarr An array of sanitized (and slashed) but otherwise unmodified post data.
* @see https://developer.wordpress.org/reference/functions/wp_insert_post/#parameters
* @return {[type]} [description]
*/
function kia_limit_posts( $data,$postarr ) {
if ( isset( $data['post_type'] ) && 'post' === $data['post_type'] ) {
$posts_per_month = 10; // Could be set as an updatable option
$user_id = get_current_user_id();
// Number of posts in current month by this user
$author_posts = new WP_Query( array(
'post_type' => 'any','post_status' => 'publish','author' => $user_id,'fields' => 'ids','monthnum' => date( 'm' ),// Whatever the current month is
) );
$author_post_count = $author_posts = $author_posts->post_count + 1; // Add current post
if( $author_post_count > $posts_per_month ) {
$data['post_status'] = 'draft';
}
}
return $data;
}
add_filter( 'wp_insert_post_data','kia_limit_posts',10,2 );
//Thank you in advance.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。