介绍使用WordPress时10个常用的MySQL查询
<p>大多数使用 WordPress 搭建的网站,其后台都是 MySQL 数据库,经常我们需要定制 WordPress 的功能,这里我们列表 10 个最有用的 WordPress 的数据库查询,你需要一个数据库的管理工具,例如 phpMyAdmin 或者 Navicat 等来执行这些 SQL 语句。</p>
<p>
<strong>1. 将所有文件的作者改为另外一个用户</strong></p>
<p>
在修改之前,你先要知道两个不同用户的 ID,你可以在 WP 后台的 Author & User 页面中找到这个 ID,或者在查看用户信息时点击用户名的链接,地址栏中出现的 user_id 对应的值就是用户ID,然后运行以下命令进行修改:<br>
</p>
<div>
<div>
<div id="highlighter_4785">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>UPDATE</code> <code>wp_posts </code><code>SET</code> <code>post_author = </code><code>'new-author-id'</code> <code>WHERE</code> <code>post_author = </code><code>'old-author-id'</code><code>;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
<strong>2. 修改默认管理员 admin 的名称</strong></p>
<p>
WP 默认安装会创建一个名为 admin 的管理员帐号,你可以修改这个帐号的名称:<br>
</p>
<div>
<div>
<div id="highlighter_388973">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>UPDATE</code> <code>wp_users </code><code>SET</code> <code>user_login = </code><code>'Your New Username'</code> <code>WHERE</code> <code>user_login = </code><code>'Admin'</code><code>;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
<strong>3. 删除修订版 Revision</strong></p>
<p>
文章修订版浪费了大量的存储资源,当你有数以千计的文章时,这个数值更加惊人,这会影响程序执行的性能、数据获取,降低页面加载时间,解决的办法就是删除这些无用的修订版信息:<br>
</p>
<div>
<div>
<div id="highlighter_131706">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<div>
4</div>
</td>
<td>
<div>
<div>
<code>DELETE</code> <code>a,b,c </code><code>FROM</code> <code>wp_posts a</code>
</div>
<div>
<code>LEFT</code> <code>JOIN</code> <code>wp_term_relationships b </code><code>ON</code> <code>(a.ID = b.object_id)</code>
</div>
<div>
<code>LEFT</code> <code>JOIN</code> <code>wp_postmeta c </code><code>ON</code> <code>(a.ID = c.post_id)</code>
</div>
<div>
<code>WHERE</code> <code>a.post_type = </code><code>'revision'</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
<strong>4. 更改 GUID</strong></p>
<p>
在进行博客移植时,你需要修复 wp_posts 表中的 URL 里的 GUID 信息,这是非常关键的,因为 GUID 用于将 URL 路径与文章信息对应起来:<br>
</p>
<div>
<div>
<div id="highlighter_671872">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>UPDATE</code> <code>wp_posts </code><code>SET</code> <code>guid = </code><code>REPLACE</code> <code>(guid, </code><code>'http://www.oldsiteurl.com'</code><code>, </code><code>'http://www.newsiteurl.com'</code><code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
<strong>5. 更改 Siteurl & Homeurl</strong></p>
<p>
当你将网站从本地移到真正的服务器,网站并无法使用,因为完整的路径依然指向 localhost,你需要修改网站的 URL 和首页 URL:<br>
</p>
<div>
<div>
<div id="highlighter_9532">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>UPDATE</code> <code>wp_options </code><code>SET</code> <code>option_value = </code><code>replace</code><code>(option_value, </code><code>'http://www.oldsiteurl.com'</code><code>, </code><code>'http://www.newsiteurl.com'</code><code>) </code><code>WHERE</code> <code>option_name = </code><code>'home'</code> <code>OR</code> <code>option_name = </code><code>'siteurl'</code><code>;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
<strong>6. 删除 Pingback 数据</strong></p>
<p>
受欢迎的文章会收到大量的 pingback 信息,这会让数据库的体积庞大,可以使用下面 SQL 语句删除:<br>
</p>
<div>
<div>
<div id="highlighter_138724">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>DELETE</code> <code>FROM</code> <code>wp_comments </code><code>WHERE</code> <code>comment_type = </code><code>'pingback'</code><code>;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
<strong>7. 更改图片路径</strong></p>
<p>
如果你使用 CDN 来处理图片访问,在创建完 CNAME 记录后,你可通过下面查询来修改所有图像的路径:<br>
</p>
<div>
<div>
<div id="highlighter_840038">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>UPDATE</code> <code>wp_posts </code><code>SET</code> <code>post_content = </code><code>REPLACE</code> <code>(post_content, </code><code>'src="http://www.oldsiteurl.com'</code><code>, </code><code>'src="http://yourcdn.newsiteurl.com'</code><code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
你还需要通过下面语句来修改图片附件的 GUID 信息:<br>
</p>
<div>
<div>
<div id="highlighter_564223">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>UPDATE</code> <code>wp_posts </code><code>SET</code> <code>guid = </code><code>REPLACE</code> <code>(guid, </code><code>'http://www.oldsiteurl.com'</code><code>, </code><code>'http://yourcdn.newsiteurl.com'</code><code>) </code><code>WHERE</code> <code>post_type = </code><code>'attachment'</code><code>;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
<strong>8. 标出无用的标签</strong></p>
<p>
删除文章时并不会保证删除对应的标签,你必须手工来做这个事情,下面这个查询可让你找出那些没有用到的标签:<br>
</p>
<div>
<div>
<div id="highlighter_866956">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
</td>
<td>
<div>
<div>
<code>SELECT</code> <code>* </code><code>From</code> <code>wp_terms wt</code>
</div>
<div>
<code>INNER</code> <code>JOIN</code> <code>wp_term_taxonomy wtt </code><code>ON</code> <code>wt.term_id=wtt.term_id </code><code>WHERE</code> <code>wtt.taxonomy=</code><code>'post_tag'</code> <code>AND</code> <code>wtt.</code><code>count</code><code>=0;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
<strong>9. 重置密码</strong></p>
<p>
如果想重置登录密码,如果嫌麻烦可直接用下面的 SQL 语句来完成:<br>
</p>
<div>
<div>
<div id="highlighter_19829">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>UPDATE</code> <code>wp_users </code><code>SET</code> <code>user_pass = MD5( </code><code>'new_password'</code> <code>) </code><code>WHERE</code> <code>user_login = </code><code>'your-username'</code><code>;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
<strong>10. 更新文章元数据</strong></p>
<p>
如果你的每篇文章都保持了特别的 URL ,可使用下面语句来处理:<br>
</p>
<div>
<div>
<div id="highlighter_581357">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>UPDATE</code> <code>wp_postmeta </code><code>SET</code> <code>meta_value = </code><code>REPLACE</code> <code>(meta_value, </code><code>'http://www.oldsiteurl.com'</code><code>,</code><code>'http://www.newsiteurl.com'</code><code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
在做任何修改之前,我们建议你对数据库做好备份后再操作。</p>
頁:
[1]