brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: wp db query runs SQL with the credentials from wp-config

Database
~/tips/tip-wordpress-db-query-select

wp db query 'SELECT option_value FROM wp_options WHERE option_name="home"' --skip-column-names

Runs a SQL query against the WordPress database without needing the database password.

When I want one value out of a WordPress database, opening phpMyAdmin or hunting for the database password feels like a lot of work. wp db query skips both, because it reads the connection details from wp-config.php itself.

Run it from the WordPress folder and give it a SQL statement:

wp db query 'SELECT option_value FROM wp_options WHERE option_name="home"' --skip-column-names

The output is the bare value, without the ASCII table borders. --skip-column-names is not a WP-CLI option, it is passed on to the mysql client, which is why it works. Leave it out and you get the normal table with headers.

If you have a longer query, keep it in a file and feed it in on standard input:

wp db query < debug.sql

The docs also show a handy check of every table at once: wp db query "CHECK TABLE $(wp db tables | paste -s -d, -);".

Two caveats. The table prefix is not always wp_, so run wp db tables first if a query says the table does not exist. And the command runs whatever you give it, so UPDATE and DELETE change the live database. Make an export with wp db export before you write anything. On a multisite network the --url flag has no effect here: you query another site by using its own prefix, such as wp_2_options.

Further reading: wp db query (WP-CLI handbook)

More Database tips mariadb -e 'CREATE DATABASE IF NOT EXISTS blog' mariadb-dump --no-create-info --insert-ignore shop orders > orders-rows.sql SELECT table_schema, table_name, CHECKSUM TABLE shop.orders; services: All Database tips (53) All topics

Link