最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

SQL Bulk update all Wordpress user's nicknames to firstname + lastname format

programmeradmin0浏览0评论

I'm trying to bulk update all my users on one of my Wordpress sites to so that their nicknames aren't the default username but have the format firstname + lastname instead.

I've put together this code and have run it in PhpMyAdmin but it doesn't seem to have changed anything (although I get no errors):

UPDATE wp_users SET user_nicename = CONCAT(
    (
        SELECT meta_value FROM wp_usermeta 
            WHERE meta_key = 'first_name' AND user_id = ID
    ),
    ' ', 
    (
        SELECT meta_value FROM wp_usermeta 
            WHERE meta_key = 'last_name' AND user_id = ID
    )
);

Can anyone see where I'm going wrong with this?

I'm trying to bulk update all my users on one of my Wordpress sites to so that their nicknames aren't the default username but have the format firstname + lastname instead.

I've put together this code and have run it in PhpMyAdmin but it doesn't seem to have changed anything (although I get no errors):

UPDATE wp_users SET user_nicename = CONCAT(
    (
        SELECT meta_value FROM wp_usermeta 
            WHERE meta_key = 'first_name' AND user_id = ID
    ),
    ' ', 
    (
        SELECT meta_value FROM wp_usermeta 
            WHERE meta_key = 'last_name' AND user_id = ID
    )
);

Can anyone see where I'm going wrong with this?

Share Improve this question edited Feb 9, 2021 at 18:07 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Feb 9, 2021 at 15:27 jamesmonk83jamesmonk83 31 bronze badge 2
  • "doesn't seem to have changed" - does it change or not? You should be able to see and test this from PHPMyAdmin – kero Commented Feb 9, 2021 at 15:33
  • Sorry I should have been more clear, in PhpMyAdmin the column has been updated but in the Wordpress user profile it's still the same as before and doesn't appear to have changed. – jamesmonk83 Commented Feb 9, 2021 at 16:36
Add a comment  | 

1 Answer 1

Reset to default 1

You are updating the wrong column. user_nicename is used for the permalink of authors (so you really should undo your change if already done so in the actual database). If you want to change how usernames are displayed, update display_name instead.

UPDATE wp_users 
SET display_name = CONCAT(
    (SELECT meta_value FROM wp_usermeta WHERE meta_key = 'first_name' AND user_id = ID),
    ' ',
    (SELECT meta_value FROM wp_usermeta WHERE meta_key = 'last_name' AND user_id = ID)
);

I'm unsure if first_name and last_name can ever be NULL. If so, this query will fail. A more robust version would be

UPDATE wp_users 
SET display_name = CONCAT(
    COALESCE((SELECT meta_value FROM wp_usermeta WHERE meta_key = 'first_name' AND user_id = ID), ''),
    ' ',
    COALESCE((SELECT meta_value FROM wp_usermeta WHERE meta_key = 'last_name' AND user_id = ID), '')
);

Using COALESCE(col, '') will avoid any problems with NULL columns.

发布评论

评论列表(0)

  1. 暂无评论