Changing URLs in WordPress MySQL Database

UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldurl', 'http://www.newurl') WHERE option_name = 'home' OR option_name = 'siteurl';

UPDATE wp_posts SET guid = replace(guid, 'http://www.oldurl','http://www.newurl');

UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldurl', 'http://www.newurl');

UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.oldurl','http://www.newurl');

This is a very common task when developing WordPress sites and there are loads of sites that show this query.

The problem is, I suffer from some kind of Google search blindness and it always takes a few tries to find the right page. Much easier to put it on my own site.

Checking if tables exist in a MySQL database

I recently needed to create an add-on for a shopping cart system which required it’s own database table and relied on database tables from other add-ons.

It needed an install method to create it’s own tables and needed to check for the existence of other tables.

Previously, I’ve needed to check for the existence of one table, which is easy enough:


And, retrieving all tables allows you to check the result for multiple tables in the application layer:


But I wondered if there might be a more elegant solution to checking for multiple tables.

The answer is to query the information_schema database directly which provides a bit more flexibility:

FROM information_schema.TABLES

The application can use the result for a simple boolean check if all required tables exist by counting the number of results and can also determine which tables need creating from the list.

The query can also be modified with more criteria, such as whether the table is a view or base table.