13
 T he W ordPress Dat ab ase D em yst i ed S inceit w as rel ea sedover a d eca de ag o, Wor dP r ess h as b eco m e t he m ost po pu lar C M S ch o i ce f o r b e g i n n e r s a n d ex p ert s a l i ke . A l t h o u g h t h e i n st a l l a t i on o f W o r d P r e ss, a n d th e a d dition o f d a t a d o e s n o t re q u ir e a ny kn o w l e d g e o f t h e u n d erl yi n g stru ct u re, yo u m i g h t n d yo u r se l f i n a si t u a t i on  w he re som e kno w l ed ge of t he da t ab ase i s r eq ui r ed .

The WordPress Database Demystified

Embed Size (px)

DESCRIPTION

The WordPress Database Demystified

Citation preview

7/18/2019 The WordPress Database Demystified

http://slidepdf.com/reader/full/the-wordpress-database-demystified 1/13

The WordPress Database

DemystifiedSince it was released over a decade ago, WordPress has become the most popular

CMS choice for beginners and experts alike. Although the installation of WordPress,

and the addition of data does not require any knowledge of the underlying structure, you

mightfind yourself in a situation where some knowledge of the database is required.

7/18/2019 The WordPress Database Demystified

http://slidepdf.com/reader/full/the-wordpress-database-demystified 2/13

Although the favorite choice of users when it comes to working with databases is

oftenphpMyAdmin, we will focus on raw SQL queries, keeping in mind that everyone

7/18/2019 The WordPress Database Demystified

http://slidepdf.com/reader/full/the-wordpress-database-demystified 3/13

may not be comfortable with phpMyAdmin (you have an option in phpMyAdmin to run

raw SQL queries too!).

Exploring the Database

As you probably know, WordPress uses MySQL. To log into MySQL, run the following in

your terminal:

mysql -u [username] -p -D [database_name]

Once you have logged into the WordPress database in MySQL, you can check the

tables that WordPress has created by running the following:

show tables;

To check the structure of any table, run:

desc [table_name];

Note that I would be describing the tables that are created by WordPress on a fresh

installation. Installing new plugins may create new tables, which I will not be covering in

this post.

Also, I will assume that your table prefix is “wp“. In case you have used a different table

 prefix, replace “wp” by your prefix in the table names.

7/18/2019 The WordPress Database Demystified

http://slidepdf.com/reader/full/the-wordpress-database-demystified 4/13

I will explain the tables in a logical order, rather than the alphabetical order that you will

find in most tutorials.

wp_options

Thewp_options table stores all the settings of your WordPress site like title, tagline and

timezone. All the options that you set in Settings in your Dashboard are stored in this

table.

wp_users, wp_usermeta

As the name suggests,wp_users stores the list of all the registered users on your

WordPress site. It contains the basic information of a user like login, password (which is

encrypted), email, time of registration, display name, status and activation key (if

required).

wp_usermeta stores the meta data (or ‘data about data’) of the users. For instance,

thelast_name of a user is saved in thewp_usermeta table, rather than

thewp_users table.

There are two fields in this table that you should know about

—meta_key andmeta_value. Plugins can store custom meta data values about users

in the meta table by using newmeta_key values.

wp_posts, wp_postmeta

wp_posts stores all the post related data of your website. All the posts (and pages), and

their revisions are available in thewp_posts table. Even navigation menu items are

stored in this table.

7/18/2019 The WordPress Database Demystified

http://slidepdf.com/reader/full/the-wordpress-database-demystified 5/13

The types of entries (posts, pages, menu items or revisions) are distinguished by

thepost_type column in the table. We will see later in this post how we can use queries

in this table to our advantage.

wp_postmeta, just like the user meta data table, contains the meta data about posts.

If you use an SEO plugin, allthe meta tag data generated for posts are stored are stored

in this table.

wp_terms, wp_term_relationships, wp_term_taxonomy

Categories and tags for posts, pages or links are stored in the tablewp_terms. A

column that this table contains is aslug — which is a string that uniquely identifies the

term, thereby used in the URL for the term. This helps in SEO as the Google Bot

searches the URL for search terms too.

wp_term_relationship links these terms to objects (posts, pages or links). It serves as

a map between these objects and the terms.

wp_term_taxonomy describes the terms in details. You can imagine it as meta data of

the terms for simplicity, just that a plugin can not add custom values to this table.

wp_comments, wp_commentmeta

wp_comments stores the comments on your posts and pages. This table also contains

unapproved comments and information about the author of the comments and nesting

of comments.wp_commentscontains meta data about comments.

An important thing to note is that if you are using a third party commenting system

likeDisqus, your comments won’t be stored in your WordPress database, but in the

servers of the commenting service.

7/18/2019 The WordPress Database Demystified

http://slidepdf.com/reader/full/the-wordpress-database-demystified 6/13

wp_links

This table contains information about custom links added to your site. It has been

deprecated, but can be enabled again by using theLinks Manager plugin.

Here is the graphical view of the database, with tables linking to each other.

7/18/2019 The WordPress Database Demystified

http://slidepdf.com/reader/full/the-wordpress-database-demystified 7/13

7/18/2019 The WordPress Database Demystified

http://slidepdf.com/reader/full/the-wordpress-database-demystified 8/13

Source:WordPress.org

Using SQL to Your Advantage

Structured Query Language (SQL) is a computer language that is used to manage data

in relational database management systems like MySQL. You can retrieve or manipulate

data from database tables using SQL. We will try to use some queries that help us in

doing certain tasks that are otherwise very difficult, or impossible, to do with the

WordPress dashboard.

 A Word of Caution

If you plan to execute the SQL queries that I am going to discuss, you should know that

the changes they make are irreversible. Therefore, you must

useB!"#,$%&&"' and(%))B*$+ .

If youB!"# a session before you execute the a command, you can always go back to

the state before the command by using(%))B*$+ .

B!"#;,, ome .uery(%))B*$+;

If, however, you want the changes to stay, you can$%&&"' the changes, and they

become permanent.

B!"#;,, ome .uery$%&&"';

7/18/2019 The WordPress Database Demystified

http://slidepdf.com/reader/full/the-wordpress-database-demystified 9/13

You could also take a backup of your WordPress database either using a database

dump or a plugin like theDB Backup before running these commands.

Now, let us look at a few tricks that we can use to make our work easier.

Change Default Usernames

You probably know that once you create a user, WordPress doesn’t allow you to change

the username. However, you can do that through SQL by running a simple command.

/0D*' wp_users ' user_login 1 2[new_username]2 34( user_login 1

2[old_username]2;

The default administrator username in WordPress is ‘admin’ and because of its

popularity, hackers often try this username. For security purposes, it’s advisable to

change it to something else.

UPDATE: This worked at the time of writing, but doesn’t since recent updates. Please

see the comments below for more information.

Change Your WordPress Password

Imagine you are the admin and you forget your password. You can easily change it, if

you have access to the database.

/0D*' wp_users ' user_pass 1 &D56 2[new_password]2 7 34(user_login 1 2[username]2;

We need to use&D5 because the passwords are not stored in clear text format, but are

encrypted.

7/18/2019 The WordPress Database Demystified

http://slidepdf.com/reader/full/the-wordpress-database-demystified 10/13

Change Post Authors in Bulk

If you want to transfer posts from one author to another, you could try it with a simple

SQL query.

/0D*' wp_posts ' post_author 1 [new_author_id] 34( post_author 1[old_author_id];

You need to first check the"D of the author from thewp_users table. However, if you

are lazy, you can try this query, which looks complex, but doesn’t require you to check

the author ID.

/0D*'  wp_posts'  post_author 1 6)$' "D 8(%& wp_users 34( user_login 12[new_author_login]2734(  post_author 1 6)$' "D 8(%& wp_users 34( user_login 1

2[old_author_login]27;

Dealing with Spam Comments

If you do not use an external service for handling comments, chances are that you get a

lot of spam comments. If you have a lot of spam, you could try usingAkismet to keep a

check on the spam. However, if you already have a lot of spam, you could try some SQL

queries on them.

You can delete all spam comments in one go using the following:

7/18/2019 The WordPress Database Demystified

http://slidepdf.com/reader/full/the-wordpress-database-demystified 11/13

D)' 8(%& wp_comments 34( comment_approved 1 2spam2;

If you want to check where the spam comments are coming from, you can try this:

)$' comment_author_"0 as ip_address9 $%/#'6:7 * count8(%& wp_comments34( comment_approved 1 2spam2!(%/0 B ip_address%(D( B count D$

It will show you a list of how many spam comments came form each IP address.

Delete All Post Revisions

As I explained earlier,wp_posts saves the current version of a post whenever you click

‘Save’. Therefore, if you clicked ‘Save’ ten times while writing a post, there would be ten

iterations of the post on your database. All revisions have thepost_type as ‘revision’.

If you are curious about how many revisions you have generated so far, run the

following. I am sure that the number will be big.

)$' $%/#'6:7 8(%& wp_posts 34( post_type 1 2revision2;

If you want to delete all such revisions of published posts, run the following command:

D)' p9 t9 m 8(%& wp_posts p)8' <%"# wp_term_relationships t %# 6p="D 1 t=ob>ect_id7

7/18/2019 The WordPress Database Demystified

http://slidepdf.com/reader/full/the-wordpress-database-demystified 12/13

)8' <%"# wp_postmeta m %# 6p="D 1 m=post_id734( p=post_type 1 2revision2

The reason I used)8' <%"# is to remove the meta data and its connection with tags

and categories.

Update Links in Posts

If you are moving to a new domain, chances are that there are many links in your posts,

linking to your old domain. Changing them manually can be a tedious job. However, a

simple SQL command may be used for this purpose.

/0D*' wp_posts ' post_content 1 (0)*$ 6post_content92[old_domain]29 2[new_domain]27;

This query searches for occurrences of your old domain in thepost_content column of

the table and replaces them with your new domain.

Conclusion

With this, we come to the end of this tutorial. We hope that this post changed your view

of how WordPress works in the background and helped you develop a clearer

understanding of the database structure. Even though there are plugins for everything

nowadays, you should think twice before installing all of them, especially when you have

an easier and faster alternative.

Do you know any other SQL tricks for WordPress? Did we miss something important?

Do let us know in the comments below!

7/18/2019 The WordPress Database Demystified

http://slidepdf.com/reader/full/the-wordpress-database-demystified 13/13