Home Backend Development PHP Tutorial 整理的一些实用WordPress后台MySQL操作命令_php技巧

整理的一些实用WordPress后台MySQL操作命令_php技巧

May 17, 2016 am 09:07 AM

不过假设你的WordPress网站上有成百上千篇文章,而你需要进行全站范围的改动, 这时从后台逐条编辑就有点费时费力了,并且犯错的几率也会提高。 最好的方法是进入WordPress的MySQL数据库执行必要的查询(改动)。 通过MySQL可以迅速地完成以上任务,为你节省更多时间。

下面要介绍的就是一些省时省力的WordPress SQL查询方法。

事先备份
WordPress数据库里存储了你精心发表的每一篇文章,来自你的读者的所有评论,以及你对自己网站进行的所有个性化设置。 因此,无论你对自己有多自信,都请记住一定要事先备份WordPress数据库。 你可以通过备份插件进行备份。

为所有文章和页面添加自定义字段
这段代码可以为WordPress数据库内所有文章和页面添加一个自定义字段。 你需要做的就是把代码中的‘UniversalCutomField‘替换成你需要的文字,然后把‘MyValue‘改成需要的值。

复制代码 代码如下:

INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT ID AS post_id, 'UniversalCustomField'
AS meta_key 'MyValue AS meta_value FROM wp_postsWHERE ID NOT IN (SELECT post_id FROM wp_postmeta WHERE meta_key = 'UniversalCustomField');

如果只需要为文章添加自定义字段,可以使用下面这段代码:
复制代码 代码如下:

INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT ID AS post_id, 'UniversalCustomField'
AS meta_key 'MyValue AS meta_value
FROM wp_posts WHERE ID NOT IN
(SELECT post_id FROM wp_postmeta WHERE meta_key = 'UniversalCustomField')`` AND post_type = 'post';

如果只需要为页面添加自定义字段,可以使用下面这段代码:
复制代码 代码如下:

INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT ID AS post_id, 'UniversalCustomField'
AS meta_key 'MyValue AS meta_value
FROM wp_posts WHERE ID NOT IN
(SELECT post_id FROM wp_postmeta WHERE meta_key = 'UniversalCustomField')AND `post_type` = 'page';

删除文章meta数据
当你安装或删除插件时,系统通过文章meta标签存储数据。 插件被删除后,数据依然会存留在post_meta表中,当然这时你已经不再需要这些数据,完全可以删除之。 记住在运行查询前把代码里的‘YourMetaKey‘替换成你需要的相应值。
复制代码 代码如下:

DELETE FROM wp_postmeta WHERE meta_key = 'YourMetaKey';

查找无用标签
如果你在WordPress数据库里执行查询删除旧文章,和之前删除插件时的情况一样,文章所属标签会留在数据库里,并且还会出现在标签列表/标签云里。 下面的查询可以帮你找出无用的标签。
复制代码 代码如下:

SELECT * From wp_terms wtINNER JOIN wp_term_taxonomy wtt ON wt.term_id=wtt.term_id WHERE wtt.taxonomy='post_tag' AND wtt.count=0;

批量删除垃圾评论
执行以下SQL命令:
复制代码 代码如下:

DELETE FROM wp_comments WHERE wp_comments.comment_approved = 'spam';

批量删除所有未审核评论
这个SQL查询会删除你的网站上所有未审核评论,不影响已审核评论。
复制代码 代码如下:

DELETE FROM wp_comments WHERE comment_approved = 0

禁止评论较早文章
指定comment_status的值为open、closed或registered_only。
此外还需要设置日期(修改代码中的2010-01-01):
复制代码 代码如下:

UPDATE wp_posts SET comment_status = 'closed' WHERE post_date

停用/激活trackback与pingback
指定comment_status的值为open、closed或registered_only。
向所有用户激活pingbacks/trackbacks:
复制代码 代码如下:

UPDATE wp_posts SET ping_status = 'open';

向所有用户禁用pingbacks/trackbacks:
复制代码 代码如下:

UPDATE wp_posts SET ping_status = 'closed';

激活/停用某一日期前的Pingbacks & Trackbacks
指定ping_status的值为open、closed或registered_only。
此外还需要设置日期(修改代码中的2010-01-01):
复制代码 代码如下:

UPDATE wp_posts SET ping_status = 'closed' WHERE post_date

删除特定URL的评论
当你发现很多垃圾评论都带有相同的URL链接,可以利用下面的查询一次性删除这些评论。%表示含有“%"符号内字符串的所有URL都将被删除。
复制代码 代码如下:

DELETE from wp_comments WHERE comment_author_url LIKE "%nastyspamurl%" ;

识别并删除“X"天前的文章
查找“X"天前的所有文章(注意把X替换成相应数值):
复制代码 代码如下:

SELECT * FROM `wp_posts` WHERE `post_type` = 'post'AND DATEDIFF(NOW(), `post_date`) > X

删除“X"天前的所有文章:
复制代码 代码如下:

DELETE FROM `wp_posts` WHERE `post_type` = 'post'AND DATEDIFF(NOW(), `post_date`) > X

删除不需要的短代码
当你决定不再使用短代码时,它们不会自动消失。你可以用一个简单的SQL查询命令删除所有不需要的短代码。 把“tweet"替换成相应短代码名称:
复制代码 代码如下:

UPDATE wp_post SET post_content = replace(post_content, '[tweet]', '' ) ;

将文章转为页面
依然只要通过PHPMyAdmin运行一个SQL查询就可以搞定:
复制代码 代码如下:

UPDATE wp_posts SET post_type = 'page' WHERE post_type = 'post'

将页面转换成文章:
复制代码 代码如下:

UPDATE wp_posts SET post_type = 'post' WHERE post_type = 'page'

更改所有文章上的作者属性
首先通过下面的SQL命令检索作者的ID:
复制代码 代码如下:

SELECT ID, display_name FROM wp_users;

成功获取该作者的新旧ID后,插入以下命令,记住用新作者ID替换NEW_AUTHOR_ID,旧作者ID替换OLD_AUTHOR_ID。
复制代码 代码如下:

UPDATE wp_posts SET post_author=NEW_AUTHOR_ID WHERE post_author=OLD_AUTHOR_ID;

批量删除文章修订历史
文章修订历史保存可以很实用,也可以很让人烦恼。 你可以手动删除修订历史,也可以利用SQL查询给自己节省时间。
复制代码 代码如下:

DELETE FROM wp_posts WHERE post_type = "revision";

停用/激活所有WordPress插件
激活某个插件后发现无法登录WordPress管理面板了,试试下面的查询命令吧,它会立即禁用所有插件,让你重新登录。
复制代码 代码如下:

UPDATE wp_options SET option_value = 'a:0:{}' WHERE option_name = 'active_plugins';

更改WordPress网站的目标URL
把WordPress博客(模板文件、上传内容&数据库)从一台服务器移到另一台服务器后,接下来你需要告诉WordPress你的新博客地址。
使用以下命令时,注意将http://www.old-site.com换成你的原URL,http://blog.doucube.com换成新URL地址。
首先:
复制代码 代码如下:

UPDATE wp_options
SET option_value = replace(option_value, 'http://www.old-site.com', 'http://blog.doucube.com')
WHERE option_name = 'home' OR option_name = 'siteurl';

然后利用下面的命令更改wp_posts里的URL:
复制代码 代码如下:

UPDATE wp_posts SET guid = replace(guid, 'http://www.old-site.com','http://blog.doucube.com);

最后,搜索文章内容以确保新URL链接与原链接没有弄混:
复制代码 代码如下:

UPDATE wp_posts SET post_content = replace(post_content, ' http://www.ancien-site.com ', ' http://blog.doucube.com ');

更改默认用户名Admin
把其中的YourNewUsername替换成新用户名。
复制代码 代码如下:

UPDATE wp_users SET user_login = 'YourNewUsername' WHERE user_login = 'Admin';

手动重置WordPress密码
如果你是你的WordPress网站上的唯一作者,并且你没有修改默认用户名, 这时你可以用下面的SQL查询来重置密码(把其中的PASSWORD换成新密码):
复制代码 代码如下:

UPDATE `wordpress`.`wp_users` SET `user_pass` = MD5('PASSWORD')
WHERE `wp_users`.`user_login` =`admin` LIMIT 1;

搜索并替换文章内容
OriginalText换成被替换内容,ReplacedText换成目标内容:
复制代码 代码如下:

UPDATE wp_posts SET `post_content` = REPLACE (`post_content`, 'OriginalText','ReplacedText');

更改图片URL
下面的SQL命令可以帮你修改图片路径:
复制代码 代码如下:

UPDATE wp_postsSET post_content = REPLACE (post_content, 'src="http://www.myoldurl.com', 'src="http://blog.doucube.com');
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

See all articles