Wp db query (WP-CLI)

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen

wp db query: Execute an SQL query against a WordPress database.

$ wp help db query


  wp db query


  Executes a SQL query against the database.


  wp db query [<sql>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaults]

  Executes an arbitrary SQL query using `DB_HOST`, `DB_NAME`, `DB_USER`
 and `DB_PASSWORD` database credentials specified in wp-config.php.


    A SQL query. If not passed, will try to read from STDIN.

    Username to pass to mysql. Defaults to DB_USER.

    Password to pass to mysql. Defaults to DB_PASSWORD.

    Extra arguments to pass to mysql. [Refer to mysql docs](https://dev.mysql.com/doc/en/mysql-command-options.html).

    Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration.


    # Execute a query stored in a file
    $ wp db query < debug.sql

    # Check all tables in the database
    $ wp db query "CHECK TABLE $(wp db tables | paste -s -d, -);"
    | Table                                 | Op    | Msg_type | Msg_text |
    | wordpress_dbase.wp_users              | check | status   | OK       |
    | wordpress_dbase.wp_usermeta           | check | status   | OK       |
    | wordpress_dbase.wp_posts              | check | status   | OK       |
    | wordpress_dbase.wp_comments           | check | status   | OK       |
    | wordpress_dbase.wp_links              | check | status   | OK       |
    | wordpress_dbase.wp_options            | check | status   | OK       |
    | wordpress_dbase.wp_postmeta           | check | status   | OK       |
    | wordpress_dbase.wp_terms              | check | status   | OK       |
    | wordpress_dbase.wp_term_taxonomy      | check | status   | OK       |
    | wordpress_dbase.wp_term_relationships | check | status   | OK       |
    | wordpress_dbase.wp_termmeta           | check | status   | OK       |
    | wordpress_dbase.wp_commentmeta        | check | status   | OK       |

    # Pass extra arguments through to MySQL
    $ wp db query 'SELECT * FROM wp_options WHERE option_name="home"' --skip-column-names
    | 2 | home | http://wordpress-develop.dev | yes |

Extra MySQL arguments

In the example above, --skip-column-name is a MySQL argument, or maybe more precisely: A mysql client option. See all of them here: https://dev.mysql.com/doc/refman/8.0/en/mysql-command-options.html

Casus: Problem with non-ASCII characters? (2022.08)

I seem to get weird results with an SQL string like this: update wp_term_taxonomy set description=replace(description, 'één-na-kortste', 'zweitkürzeste');

Remarkably, this works just fine directly in MySQL Workbench. Maybe as a workaround: As soon as there are non-ASCII characters in a string, execute it directly through mysql? That seems a bit silly

Casus: Retrieve all term-id of a taxonomy (2022.10)

I feel perplexed that there seem to be no obvious way to be to iterate over a collection of objects, like posts or taxonomy terms. To add insult to injury: WP-CLI commands to return object-IDs often are limited to max. 100 items at a time.

Currently, this seems a quite good approach:

i='select term_id from wp_terms join wp_term_taxonomy using (term_id) where taxonomy like "pa_as%"'
mapfile -t j < <( wp db query "$i" --skip-column-names )

echo ${j[@]}
echo "# of entries: ${#j[@]}"
echo ${!j[@]}
echo "Entry 5: ${j[5]}"

For details, see Iterate over objects (WPI-CLI).

See also