Home Database Mysql Tutorial 介绍使用WordPress时10个常用的MySQL查询_MySQL

介绍使用WordPress时10个常用的MySQL查询_MySQL

Jun 01, 2016 pm 01:00 PM
mysql

大多数使用 WordPress 搭建的网站,其后台都是 MySQL 数据库,经常我们需要定制 WordPress 的功能,这里我们列表 10 个最有用的 WordPress 的数据库查询,你需要一个数据库的管理工具,例如  phpMyAdmin 或者  Navicat 等来执行这些 SQL 语句。

1. 将所有文件的作者改为另外一个用户

在修改之前,你先要知道两个不同用户的 ID,你可以在 WP 后台的 Author & User 页面中找到这个 ID,或者在查看用户信息时点击用户名的链接,地址栏中出现的 user_id 对应的值就是用户ID,然后运行以下命令进行修改:
 

UPDATE wp_posts SET post_author = 'new-author-id' WHERE post_author = 'old-author-id';
Copy after login

2. 修改默认管理员 admin 的名称

WP 默认安装会创建一个名为 admin 的管理员帐号,你可以修改这个帐号的名称:

UPDATE wp_users SET user_login = 'Your New Username' WHERE user_login = 'Admin';
Copy after login

3. 删除修订版 Revision

文章修订版浪费了大量的存储资源,当你有数以千计的文章时,这个数值更加惊人,这会影响程序执行的性能、数据获取,降低页面加载时间,解决的办法就是删除这些无用的修订版信息:

DELETE a,b,c FROM wp_posts a
LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id)
LEFT JOIN wp_postmeta c ON (a.ID = c.post_id)
WHERE a.post_type = 'revision'
Copy after login

4. 更改 GUID

在进行博客移植时,你需要修复 wp_posts 表中的 URL 里的 GUID 信息,这是非常关键的,因为 GUID 用于将 URL 路径与文章信息对应起来:

UPDATE wp_posts SET guid = REPLACE (guid, 'http://www.oldsiteurl.com', 'http://www.newsiteurl.com');
Copy after login

5. 更改 Siteurl & Homeurl

当你将网站从本地移到真正的服务器,网站并无法使用,因为完整的路径依然指向 localhost,你需要修改网站的 URL 和首页 URL:

UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldsiteurl.com', 'http://www.newsiteurl.com') WHERE option_name = 'home' OR option_name = 'siteurl';
Copy after login

6. 删除 Pingback 数据

受欢迎的文章会收到大量的 pingback 信息,这会让数据库的体积庞大,可以使用下面 SQL 语句删除:

DELETE FROM wp_comments WHERE comment_type = 'pingback';
Copy after login

7. 更改图片路径

如果你使用 CDN 来处理图片访问,在创建完 CNAME 记录后,你可通过下面查询来修改所有图像的路径:

UPDATE wp_posts SET post_content = REPLACE (post_content, 'src="http://www.oldsiteurl.com', 'src="http://yourcdn.newsiteurl.com');
Copy after login

你还需要通过下面语句来修改图片附件的 GUID 信息:

UPDATE wp_posts SET guid = REPLACE (guid, 'http://www.oldsiteurl.com', 'http://yourcdn.newsiteurl.com') WHERE post_type = 'attachment';
Copy after login

8. 标出无用的标签

删除文章时并不会保证删除对应的标签,你必须手工来做这个事情,下面这个查询可让你找出那些没有用到的标签:

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

9. 重置密码

如果想重置登录密码,如果嫌麻烦可直接用下面的 SQL 语句来完成:

UPDATE wp_users SET user_pass = MD5( 'new_password' ) WHERE user_login = 'your-username';
Copy after login

10. 更新文章元数据

如果你的每篇文章都保持了特别的 URL ,可使用下面语句来处理:

UPDATE wp_postmeta SET meta_value = REPLACE (meta_value, 'http://www.oldsiteurl.com','http://www.newsiteurl.com');
Copy after login

在做任何修改之前,我们建议你对数据库做好备份后再操作。

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 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)

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

See all articles