From b4901f6f2af4ed6513f0ed1c3caf8746385af787 Mon Sep 17 00:00:00 2001 From: Jay McPartland Date: Thu, 13 Jun 2024 15:52:53 +0200 Subject: [PATCH 1/3] docs: add documentation for WordPress.DB.DirectDatabaseQuery --- .../Docs/DB/DirectDatabaseQueryStandard.xml | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 WordPress/Docs/DB/DirectDatabaseQueryStandard.xml diff --git a/WordPress/Docs/DB/DirectDatabaseQueryStandard.xml b/WordPress/Docs/DB/DirectDatabaseQueryStandard.xml new file mode 100644 index 0000000000..c1312e7fc8 --- /dev/null +++ b/WordPress/Docs/DB/DirectDatabaseQueryStandard.xml @@ -0,0 +1,119 @@ + + + + + + + + 'foo', + ] +); + ]]> + + + get_results( + $wpdb->prepare( + "SELECT * FROM x WHERE y = %s", + 'foo', + ), +); + ]]> + + + + + + + + + query( + 'SELECT x FROM foo' + ); + + wp_cache_set( 'foo', $results ); + return $results; +} + ]]> + + + query( 'SELECT x FROM foo' ); +} + ]]> + + + + + query( + 'SELECT x FROM foo' + ); + + wp_cache_set( 'foo', $results ); + return $results; +} + ]]> + + + query( + 'SELECT * from foo' + ); + + wp_cache_set( 'foo', $results ); + return $results; +} + ]]> + + + + + + + + + + + + query( 'CREATE TABLE foo' ); + ]]> + + + From 910738f37e9089aca93a6ba05e40a72c1ef7f541 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Mon, 9 Feb 2026 15:16:26 -0300 Subject: [PATCH 2/3] Improve WordPress.DB.DirectDatabaseQuery documentation - Rewrite standard descriptions to explain why each rule exists. - Add tags to highlight key parts in code examples. - Use realistic code examples. - Replace second caching example with a write + cache invalidation pattern. - Wrap caching examples in functions, as the sniff always generates a NoCaching warning if the caching code is not inside a function. - Remove dbDelta() recommendation from SchemaChange section. I believe the original intent of the rule is to discourage schema changes and not suggest dbDelta() is used. - Remove SchemaChange code comparison (no valid alternative). --- .../Docs/DB/DirectDatabaseQueryStandard.xml | 100 ++++++++---------- 1 file changed, 45 insertions(+), 55 deletions(-) diff --git a/WordPress/Docs/DB/DirectDatabaseQueryStandard.xml b/WordPress/Docs/DB/DirectDatabaseQueryStandard.xml index c1312e7fc8..7b9c12b700 100644 --- a/WordPress/Docs/DB/DirectDatabaseQueryStandard.xml +++ b/WordPress/Docs/DB/DirectDatabaseQueryStandard.xml @@ -5,115 +5,105 @@ > 'foo', - ] +$page_query = new WP_Query( + array( + 'post_type' => 'page', + ) ); ]]> get_results( +$results = $wpdb->get_results( $wpdb->prepare( - "SELECT * FROM x WHERE y = %s", - 'foo', - ), + "SELECT * FROM %i + WHERE post_type = %s", + $wpdb->posts, + 'page' + ) ); ]]> - - + wp_cache_get( $key ); + if ( false !== $cached ) { return $cached; } - $results = $wpdb->query( - 'SELECT x FROM foo' + $results = $wpdb->get_col( + "SELECT ID FROM $wpdb->posts + WHERE post_status = 'draft'" ); - wp_cache_set( 'foo', $results ); + wp_cache_set( $key, $results ); + return $results; } ]]> - + query( 'SELECT x FROM foo' ); +function get_draft_ids() { + $results = $wpdb->get_col( + "SELECT ID FROM $wpdb->posts + WHERE post_status = 'draft'" + ); + + return $results; } ]]> - + query( - 'SELECT x FROM foo' + $wpdb->update( + $wpdb->posts, + array( 'post_title' => $title ), + array( 'ID' => $post_id ) ); - wp_cache_set( 'foo', $results ); - return $results; + clean_post_cache( $post_id ); } ]]> - + query( - 'SELECT * from foo' - ); - wp_cache_set( 'foo', $results ); - return $results; + $wpdb->update( + $wpdb->posts, + array( 'post_title' => $title ), + array( 'ID' => $post_id ) + ); } ]]> - - - - - - - query( 'CREATE TABLE foo' ); - ]]> - - From 2a8fc578cc114392ef198b37671ea75656f7e3bb Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Thu, 26 Feb 2026 09:01:13 -0300 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Juliette <663378+jrfnl@users.noreply.github.com> --- .../Docs/DB/DirectDatabaseQueryStandard.xml | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/WordPress/Docs/DB/DirectDatabaseQueryStandard.xml b/WordPress/Docs/DB/DirectDatabaseQueryStandard.xml index 7b9c12b700..d727c064e8 100644 --- a/WordPress/Docs/DB/DirectDatabaseQueryStandard.xml +++ b/WordPress/Docs/DB/DirectDatabaseQueryStandard.xml @@ -5,7 +5,7 @@ > @@ -103,7 +103,22 @@ function update_post_title() { + + + + + + query( + "ALTER TABLE {$wpdb->posts} + ADD COLUMN rating int" +); + ]]> + +