我是一个多作者博客的管理员。我正在实施一个监控系统,如果我认为某个用户是垃圾邮件帐户,或者他们违反了网站规则,我需要将该用户从“作者”(此角色允许他们发表自定义帖子)降级为“读者”角色。
在使用管理屏幕更改了他们的角色后,我如何才能使他们发表的所有帖子自动删除,而不必滚动并自己查找它们?
非常感谢
EDIT using advice from answers below:
add_action( \'set_user_role\', \'wpse98904_remove_demoted_user_posts\', 10, 2 );
function wpse98904_remove_demoted_user_posts( $demoted_author_id, $role ) {
// In here you\'d search for all the posts by the user
$args = array(
\'numberposts\' => -1,
\'author\' => $demoted_author_id,
\'post_type\' => \'custom_post\',
// So if your custom post type is named \'xyz_book\', then use:
// \'post_type\' => \'xyz_book\',
);
$demoted_author_posts = get_posts( $args );
foreach( $demoted_author_posts as $p ) {
// delete the post (actually Trash it)
if($role == \'subscriber\') {
wp_trash_post( $p->ID);
// To force the deletion (ie, bypass the Trash):
// wp_delete_post( $p->ID, true );
}
}
}
我使用wp\\u trash\\u post来丢弃事件,因为在wp\\u delete\\u post中添加“false”对我不起作用。
最合适的回答,由SO网友:Pat J 整理而成
您可以将操作添加到set_user_role
钩子:
add_action( \'set_user_role\', \'wpse98904_remove_demoted_user_posts\', 10, 2 );
function wpse98904_remove_demoted_user_posts( $demoted_author_id, $role ) {
if( \'subscriber\' == $role ) {
// In here you\'d search for all the posts by the user
$args = array(
\'numberposts\' => -1,
\'author\' => $demoted_author_id,
\'post_type\' => \'{your custom post type name}\',
// So if your custom post type is named \'xyz_book\', then use:
// \'post_type\' => \'xyz_book\',
);
$demoted_author_posts = get_posts( $args );
foreach( $demoted_author_posts as $p ) {
// delete the post (actually Trash it)
wp_delete_post( $p->ID );
// To force the deletion (ie, bypass the Trash):
// wp_delete_post( $p->ID, true );
}
}
}
参考
set_user_role
钩子——抄本
set_user_role
挂钩WP Trac
wp_delete_post()
-- 法典
SO网友:Andrew Bartel
WP\\u User对象通过启动set\\u role()函数来更改用户角色。在wp第815行功能结束时,包括/能力。php有一个要挂接的操作:do_action( \'set_user_role\', $this->ID, $role );
所以,在你的函数中。php或在插件中,您可以在用户功能更新后钩子触发时获取该数据,并使用wp_delete_post.
add_action(\'set_user_role\',\'myfunc\',10,2);
function myfunc($user_id,$role) {
if($role == \'subscriber\') { // or whatever you want
$posts = get_posts(\'numberposts=-1&author=\'.$user_id);
foreach($posts as $post) {
wp_delete_post($post->ID,true);
}
}
}
现在,请小心,因为按原样,此片段将永久删除帖子。如果只想将其移动到垃圾箱,请将第二个参数或wp\\u delete\\u post更改为false。