根据发布的帖子数更新用户角色

时间:2012-05-27 作者:pixelngrain

我正在尝试根据用户发布新帖子时发布的帖子数更新用户角色。我已经尝试过这个代码,但它不起作用。

    add_action(\'publish_post\', \'update_roles\');

    function update_roles()
    {

       global $wpdb;

       // Get the author
       $author = wp_get_current_user();

        $posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_author = " . $author->ID );

        $numPost = count($posts);

        // Do the checks to see if they have the roles and if not update them.
        if($numPost > 0 && $numPosts <= 2 && current_user_can(\'subscriber\'))
        {

            $user_id_role = new WP_User($user_id);
            $user_id_role->set_role(\'contributor\'); 

        } elseif ($numPost > 3 && $numPosts <= 5 && current_user_can(\'contributor\'))
        {
            $user_id_role = new WP_User($user_id);
            $user_id_role->set_role(\'author\'); 

        } elseif ($numPost > 6 && $numPosts <= 9 && current_user_can(\'author\'))
        {

            $user_id_role = new WP_User($user_id);
            $user_id_role->set_role(\'author\'); 

        }


    }

?>
我也尝试过这个,但它也不起作用。

<?php

    add_action(\'publish_post\', \'update_roles\');

    function update_roles()
    {

       global $wpdb;

       // Get the author
       $author = wp_get_current_user();


       // Get post by author
       $posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_author = " . $author->ID );

       $numPost = count($posts);

       // Do the checks to see if they have the roles and if not update them.
       if($numPost > 0 && $numPosts <= 2 && current_user_can(\'subscriber\'))
       {
           // Remove role
           $author->remove_role( \'subscriber\' );

           // Add role
           $author->add_role( \'contributor\' );

       } elseif ($numPost > 3 && $numPosts <= 5 && current_user_can(\'contributor\'))
       {
           // Remove role
           $author->remove_role( \'contributor\' );

           // Add role
           $author->add_role( \'author\' );

       } elseif ($numPost > 6 && $numPosts <= 9 && current_user_can(\'author\'))
       {
           // Remove role
           $author->remove_role( \'author\' );

           // Add role
           $author->add_role( \'editor\' );

       }


    }

?>
EDIT NEW: 你能告诉我我做得对不对吗?

<?php

    add_action(\'publish_post\', \'update_roles\');

    function update_roles()
    {

       global $wpdb;

       // Get the author
       $author = wp_get_current_user();

       // Get post by author
       $posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_author = " . $author->ID );

       $numPost = count($posts);

       // Do the checks to see if they have the roles and if not update them.
       if ($numPost > 3 && $numPosts <= 5 && array_key_exists( \'contributor\', $old_role ) )
        {
            $user_id_role = new WP_User($user_id);
            $user_id_role->set_role(\'author\'); 

        } elseif ($numPost > 6 && $numPosts <= 9 && array_key_exists( \'author\', $old_role ) )
        {

            $user_id_role = new WP_User($user_id);
            $user_id_role->set_role(\'editor\'); 

        }


    }

?>

1 个回复
最合适的回答,由SO网友:Chris_O 整理而成

current_user_can() 检查是否存在capability, 例如:编辑文章,而不是角色

订阅服务器仅有的功能是读取的,这只允许他们访问仪表板以更改其配置文件(unless you added additional caps)。他们甚至不能发布帖子,所以你必须从贡献者开始。

$old_role = get_user_meta( $user_id, \'wp_capabilities\' );
elseif ($numPost > 3 && $numPosts <= 5 && array_key_exists( \'contributor\', $old_role ) )
    {
        $user_id_role = new WP_User($user_id);
        $user_id_role->set_role(\'author\'); 

    } elseif ($numPost > 6 && $numPosts <= 9 && array_key_exists( \'author\', $old_role ) )
    {

        $user_id_role = new WP_User($user_id);
        $user_id_role->set_role(\'author\'); 

    }
我们正在做的是使用get_user_meta() 从usermeta表检索wp\\U功能的值。因为该字段的值是数组:array([贡献者]=>;1)

角色是我们可以使用的关键之一php array_key_exists() 函数检查该用户是否存在该角色。此外,还可以使用count_user_posts() 函数获取post计数。

完整示例:

Update: 这是经过充分测试和工作。

    add_action( \'save_post\', \'update_roles\' );
    function update_roles( $post_id ) {
        if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
            return $post_id;

        // Get the author
        $author = wp_get_current_user();

        // Set variables for current user information
        $count = count_user_posts( $author->ID );
        $current_role = (array) get_user_meta( $author->ID, \'wp_capabilities\' );
        
        // Do the checks to see if they have the roles and if not update them.
        if ( ( $count > 3 && $count <= 5 ) && ( array_key_exists( \'contributor\', $current_role[0] ) ) ) {
            $user_id_role = new WP_User( $author->ID );
            $user_id_role->set_role( \'author\' );

        } elseif ( ( $count > 6 && $count <= 9 ) && ( array_key_exists( \'author\', $current_role[0] ) ) ) {

            $user_id_role = new WP_User( $author->ID );
            $user_id_role->set_role( \'editor\' );

        } return $post_id;

    }
    

结束

相关推荐

Adding paging to get_posts()

我正在尝试将分页添加到下面的查询中。然而,我似乎无法让寻呼正常工作。它显示上一个/下一个链接,但是,它们总是显示相同的2篇文章。此查询中有8个帖子。//$paged = get_query_var( \'page\' ); wp_reset_query(); global $post; $args = array(\'posts_per_page\' => 2,\'paged\'=>1); $posts = get_posts($args);