具有“作者”角色的 WP 用户可以发布文章。在有问题的博客上,我要求这些用户的文章必须立即发布,但不能公开显示(即,对于匿名访问者或订阅者)。我们使用 WP 3.0.5。
我们已经运行了一个插件,它允许对匿名和订阅者隐藏类别。所以到目前为止我想出的最直接的方法是:作者的新博客文章应该自动归入一个类别。然后我对匿名用户隐藏该类别。
有人知道吗:
a) 如何将“作者”用户的文章自动归入某个类别,或
b) 对于这些帖子,如何更优雅地实现“直播但不公开”的要求?
(也欢迎插件建议。)
具有“作者”角色的 WP 用户可以发布文章。在有问题的博客上,我要求这些用户的文章必须立即发布,但不能公开显示(即,对于匿名访问者或订阅者)。我们使用 WP 3.0.5。
我们已经运行了一个插件,它允许对匿名和订阅者隐藏类别。所以到目前为止我想出的最直接的方法是:作者的新博客文章应该自动归入一个类别。然后我对匿名用户隐藏该类别。
有人知道吗:
a) 如何将“作者”用户的文章自动归入某个类别,或
b) 对于这些帖子,如何更优雅地实现“直播但不公开”的要求?
(也欢迎插件建议。)
您可能想要做的是在主题functions.php
文件中编写函数来执行此操作,然后add_action
在保存帖子时使用触发该函数。
例如:
function update_category_on_save($post_id) {
// Get post
$post = wp_get_single_post($post_id)
// Map author IDs to category IDs
$mapping = array(
1 => array(123),
2 => array(234),
);
// Update the post
$new_category = $mapping[$post->post_author];
$u_post = array();
$u_post['ID'] = $post_id;
$u_post['post_category'] = $new_category;
// Only update if category changed
if($post->post_category != $new_category[0]) {
wp_update_post($u_post);
}
}
add_action('category_save_pre', 'update_category_on_save');
希望这是有道理的,并为您提供有关如何执行此操作的提示-恐怕我无法对其进行测试。
以下代码将自动将作者发布的帖子更改为私有。
function change_author_posts_to_private( $post_status ) {
// if the user is just saving a draft, we want to keep it a draft
if ( $post_status == 'draft' )
return $post_status;
$this_user = new WP_User( $_POST[ 'post_author' ] );
// this is assuming the user has just one role, which is standard
if ( $this_user->roles[0] == 'author' )
return 'private';
else
return $post_status;
}
add_filter( 'status_save_pre', 'change_author_posts_to_private' );
它过滤帖子保存的状态,从帖子变量中查看作者是谁,获取他们的第一个角色并查看它是否是作者,如果是,则返回“私人”,否则返回自然状态。当您可以直接在此处执行时,无需使用类别来执行此操作。