我博客中的一些作者每次发帖时都必须将自己的帖子添加到特定的类别中。
有时他们会忘记这一点,因此我想知道是否有任何方法可以自动检查和修复这一问题。
下面是我想要的结果的一个例子:
作者X的帖子应始终分配给A类作者Y的帖子应始终分配给B类作者Z的帖子应始终分配给C类作者如何做到这一点?
我博客中的一些作者每次发帖时都必须将自己的帖子添加到特定的类别中。
有时他们会忘记这一点,因此我想知道是否有任何方法可以自动检查和修复这一问题。
下面是我想要的结果的一个例子:
作者X的帖子应始终分配给A类作者Y的帖子应始终分配给B类作者Z的帖子应始终分配给C类作者如何做到这一点?
解决方案非常简单。并细分为两个单独的过程-查询帖子和修改其类别术语。
Querying the posts
get_posts
是一个返回帖子的函数。可以在中查看完整的参数列表WP_Query parameters要获取特定作者的所有帖子,请执行以下操作:
get_posts( \'numberposts=1&author_name=john\' );
在每个条目上循环,如下所示:foreach ( get_posts( \'author_name=john\' ) as $post ) {
// set categories...
}
Setting category terms
wp_set_object_terms
是您将要查看和使用的函数。wp_set_object_terms( $post->ID, \'John\'s Category\', \'category\', false );
Putting it together
我可能会选择这样的方式:global $wpdb; // get the global db object
foreach ( (array) $wpdb->get_results("SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = \'post\' GROUP BY post_author") as $row ) {
$author = get_userdata( $row->post_author ); // get all author data
foreach ( get_posts( \'numberposts=-1&author=\'.$row->post_author ) as $post ) {
switch ( $author->username ) {
case \'joe\':
$category = \'Joe\'s Entries\';
...
}
// set categories
wp_set_object_terms( $post->ID, $category, \'category\', false );
}
}
我相信有一些方法可以进一步优化它。运行此操作一次以修复问题。然后附加到save_post
只为将来设置类别部分。我假设您没有使用自定义帖子类型。下面的代码成功地将每个新帖子(使用publish\\u post操作)添加到指定类别,但不包括使用其他指定类别分类的帖子。您可以简单地调整或删除if语句以满足您的特定需要。
function add_category_automatically($post_ID) {
global $wpdb;
if(!in_category(\'specified-category\')){
$cat = array(9547);
wp_set_object_terms($post_ID, $cat, \'category\', true);
}
}
add_action(\'publish_post\', \'add_category_automatically\');
您将要使用save_post
钩子,每次保存或更新帖子时都会触发。
回调应该(可选)检查是否设置了类别(忽略“未分类”),否则根据作者使用作者ID将类别设置为类别段塞数组。
以下内容未经测试
function wpse45398_save($post_id,$post) {
// verify this is not an auto save routine.
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) return;
//You should check nonces and permissions here
//Get category slugs associated with post
$cats = wp_list_pluck(get_the_category($post_id),\'slug\');
$cats = array_values($cats);
//Remove \'uncategorised\' from array
unset($cats[\'uncategorised\']);
//If $cats array is empty
if(empty($cats)):
//Array of author ID to \'default category\'
$authorCat = array(
\'1\'=>\'mycatslug\',
\'2\'=>\'myothercat\',
\'3\'=>\'anothercat\'
);
$author_id=intval($post->post_author);
$defaultCat = isset($authorCat[$author_id]) ? $author_id : 0;
if(!empty($defaultCat))
wp_set_object_terms( $post_id, \'category\', $defaultCat, true )
endif;
return;
}
add_action(\'save_post\',\'wpse45398_save\',10,2);
如果你想“修复”已经存在的帖子,请参阅@Soulseekah的答案,尽管这种方法可以通过更新现有帖子来实现。上述代码将确保未来的任何职位至少属于一个类别(根据作者)。或者,publish_post
仅当帖子保存且状态为“发布”时,才能使用该函数启动回调函数(因此对于草稿,不会发生“更正”)。
看看我的插件Author Category
您也可以使用此插件对单个用户或角色设置限制,以发布到特定类别。
编辑:该插件非常容易使用,值得花时间查看链接。但是,我仍将向您展示如何使用它。安装插件后,您可以进入设置>限制类别以访问其设置。在这里,您将看到如下内容:
正如您所看到的,从这里,您可以决定哪些角色或用户可以发布到哪些类别和内容。例如,当您仅将“大学更新”类别分配给“作者”角色时,在发布新帖子时,“作者”角色用户只能看到“大学更新”类别,即用户只能发布到该类别。如果分配了多个类别,则用户在发布时可以在“类别”部分中看到多个类别。我希望这能很好地解释如何使用它。
我以为下面的代码可以用。。。但没有骰子。类似于“AND post\\u count>0”函数贡献者(){全局$wpdb;$authors = $wpdb->get_results(\"SELECT ID, user_nicename from $wpdb->users WHERE display_name <> \'admin\' AND post_count > 0 ORDER BY display_name\"); foreach ($author