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

整理的一些实用WordPress后台MySQL操作命令_PHP

Jun 01, 2016 pm 12:08 PM

WordPress

不过假设你的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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks 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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles