Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions features/db-search.feature
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,37 @@ Feature: Search through the database
And STDOUT should match /\d tables? skipped:.*wp_term_relationships/
And STDERR should be empty

Scenario: Search with exclude revisions option
Given a WP install
And I run `wp post create --post_content="This is the original post content." --post_title="Original Post" --post_status=publish --porcelain`
And save STDOUT as {POST_ID}
And I run `wp post update {POST_ID} --post_content="This is the updated post content."`

When I run `wp db search "updated post content" wp_posts --format=count`
Then STDOUT should be:
"""
2
"""

When I run `wp db search "updated post content" wp_posts --exclude_revisions --format=ids`
Then STDOUT should be:
"""
{POST_ID}
"""

When I run `wp db search "updated post content" --exclude_revisions`
Then STDOUT should contain:
"""
{POST_ID}:This is the updated post content.
"""
And STDERR should be empty

When I run `wp db search "updated post content" wp_posts --exclude_revisions --regex --format=count`
Then STDOUT should be:
"""
1
"""

@skip-windows
Scenario: Search with custom colors
Given a WP install
Expand Down
16 changes: 14 additions & 2 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,9 @@ public function prefix() {
* - count
* ---
*
* [--exclude_revisions]
* : Exclude revisions from the search.
*
* The percent color codes available are:
*
* | Code | Color
Expand Down Expand Up @@ -1739,6 +1742,7 @@ public function search( $args, $assoc_args ) {
$stats = Utils\get_flag_value( $assoc_args, 'stats', false );
$fields = Utils\get_flag_value( $assoc_args, 'fields' );
$format = Utils\get_flag_value( $assoc_args, 'format' );
$exclude_revisions = Utils\get_flag_value( $assoc_args, 'exclude_revisions', false );

$column_count = 0;
$row_count = 0;
Expand Down Expand Up @@ -1803,14 +1807,22 @@ public function search( $args, $assoc_args ) {
$primary_key_sql = self::esc_sql_ident( $primary_key ) . ', ';
}

// Skip revisions in any posts table (including other sites' tables in a multisite network).
$revisions_sql = '';
if ( $exclude_revisions && preg_match( '/posts$/', $table ) && in_array( 'post_type', $all_columns, true ) ) {
$revisions_sql = ' ' . self::esc_sql_ident( 'post_type' ) . " <> 'revision'";
}

foreach ( $text_columns as $column ) {
$column_sql = self::esc_sql_ident( $column );
if ( $regex ) {
Comment on lines 1816 to 1818

Copilot AI Mar 12, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$post_type_sql is computed for every text column, but it never changes and is only needed when $exclude_revisions applies. Moving this identifier escaping outside the inner $text_columns loop (or only computing it when needed) would reduce repeated work and make the control flow easier to follow.

Copilot uses AI. Check for mistakes.
$where_sql = $revisions_sql ? " WHERE{$revisions_sql}" : '';
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Escaped through esc_sql_ident/esc_like.
$results = $wpdb->get_results( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql}" );
$results = $wpdb->get_results( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql}{$where_sql}" );
} else {
$where_sql = $revisions_sql ? " AND{$revisions_sql}" : '';
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Escaped through esc_sql_ident/esc_like.
$results = $wpdb->get_results( $wpdb->prepare( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql} WHERE {$column_sql} LIKE %s;", $esc_like_search ) );
$results = $wpdb->get_results( $wpdb->prepare( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql} WHERE {$column_sql} LIKE %s{$where_sql};", $esc_like_search ) );
}
if ( $results ) {
$row_count += count( $results );
Expand Down