On this page
wpdb::get_results( string $query = null, string $output = OBJECT ): array|object|null
Retrieves an entire SQL result set from the database (i.e., many rows).
Description
Executes a SQL query and returns the entire SQL result.
Parameters
$querystring Optional-
SQL query.
Default:
null $outputstring Optional-
Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants.
With one of the first three, return an array of rows indexed from 0 by SQL result row number. Each row is an associative array (column => value, ...), a numerically indexed array (0 => value, ...), or an object ( ->column = value ), respectively. With OBJECT_K, return an associative array of row objects keyed by the value of each row's first column's value. Duplicate keys are discarded.Default:
OBJECT
Return
array|object|null Database query results.
Source
File: wp-includes/class-wpdb.php. View all references
public function get_results( $query = null, $output = OBJECT ) {
$this->func_call = "\$db->get_results(\"$query\", $output)";
if ( $query ) {
if ( $this->check_current_query && $this->check_safe_collation( $query ) ) {
$this->check_current_query = false;
}
$this->query( $query );
} else {
return null;
}
$new_array = array();
if ( OBJECT === $output ) {
// Return an integer-keyed array of row objects.
return $this->last_result;
} elseif ( OBJECT_K === $output ) {
// Return an array of row objects with keys from column 1.
// (Duplicates are discarded.)
if ( $this->last_result ) {
foreach ( $this->last_result as $row ) {
$var_by_ref = get_object_vars( $row );
$key = array_shift( $var_by_ref );
if ( ! isset( $new_array[ $key ] ) ) {
$new_array[ $key ] = $row;
}
}
}
return $new_array;
} elseif ( ARRAY_A === $output || ARRAY_N === $output ) {
// Return an integer-keyed array of...
if ( $this->last_result ) {
foreach ( (array) $this->last_result as $row ) {
if ( ARRAY_N === $output ) {
// ...integer-keyed row arrays.
$new_array[] = array_values( get_object_vars( $row ) );
} else {
// ...column name-keyed row arrays.
$new_array[] = get_object_vars( $row );
}
}
}
return $new_array;
} elseif ( strtoupper( $output ) === OBJECT ) {
// Back compat for OBJECT being previously case-insensitive.
return $this->last_result;
}
return null;
}
Related
Uses
| Uses | Description |
|---|---|
| wpdb::check_safe_collation() wp-includes/class-wpdb.php | Checks if the query is accessing a collation considered safe on the current version of MySQL. |
| wpdb::query() wp-includes/class-wpdb.php | Performs a database query, using current database connection. |
Used By
| Used By | Description |
|---|---|
| WP_Site_Health::should_suggest_persistent_object_cache() wp-admin/includes/class-wp-site-health.php | Determines whether to suggest using a persistent object cache. |
| WP_Debug_Data::get_database_size() wp-admin/includes/class-wp-debug-data.php | Fetches the total size of all the database tables for the active database user. |
| wp_is_site_initialized() wp-includes/ms-site.php | Checks whether a site is initialized. |
| WP_Privacy_Requests_Table::get_request_counts() wp-admin/includes/class-wp-privacy-requests-table.php | Count number of requests for each status. |
| has_term_meta() wp-includes/taxonomy.php | Gets all meta data, including meta IDs, for the given term ID. |
| WP_Term_Query::get_terms() wp-includes/class-wp-term-query.php | Retrieves the query results. |
| _prime_term_caches() wp-includes/taxonomy.php | Adds any terms from the given IDs to the cache that do not already exist in cache. |
| _prime_network_caches() wp-includes/ms-network.php | Adds any networks from the given IDs to the cache that do not already exist in cache. |
| _prime_site_caches() wp-includes/ms-site.php | Adds any sites from the given IDs to the cache that do not already exist in cache. |
| wxr_term_meta() wp-admin/includes/export.php | Outputs term meta XML tags for a given term object. |
| WP_Term::get_instance() wp-includes/class-wp-term.php | Retrieve WP_Term instance. |
| _prime_comment_caches() wp-includes/comment.php | Adds any comments from the given IDs to the cache that do not already exist in cache. |
| _wp_batch_split_terms() wp-includes/taxonomy.php | Splits a batch of shared taxonomy terms. |
| wpdb::get_table_charset() wp-includes/class-wpdb.php | Retrieves the character set for the given table. |
| maybe_convert_table_to_utf8mb4() wp-admin/includes/upgrade.php | If a table only contains utf8 or utf8mb4 columns, convert it to utf8mb4. |
| attachment_url_to_postid() wp-includes/media.php | Tries to convert an attachment URL into a post ID. |
| check_column() wp-admin/install-helper.php | Checks that database table column matches the criteria. |
| export_date_options() wp-admin/export.php | Create the date options fields for exporting a given post type. |
| export_wp() wp-admin/includes/export.php | Generates the WXR export file for download. |
| wxr_authors_list() wp-admin/includes/export.php | Outputs list of authors with posts. |
| get_editable_authors() wp-admin/includes/deprecated.php | Gets author users who can edit posts. |
| get_others_unpublished_posts() wp-admin/includes/deprecated.php | Retrieves editable posts from other users. |
| WP_List_Table::months_dropdown() wp-admin/includes/class-wp-list-table.php | Displays a dropdown for filtering items in the list table by month. |
| pre_schema_upgrade() wp-admin/includes/upgrade.php | Runs before the schema is upgraded. |
| upgrade_network() wp-admin/includes/upgrade.php | Executes network-level upgrade routines. |
| get_alloptions_110() wp-admin/includes/upgrade.php | Retrieve all options as it was for 1.2. |
| dbDelta() wp-admin/includes/upgrade.php | Modifies the database based on specified SQL statements. |
| get_users_drafts() wp-admin/includes/user.php | Retrieve the user’s drafts. |
| parent_dropdown() wp-admin/includes/template.php | Prints out option HTML elements for the page parents drop-down. |
| media_upload_library_form() wp-admin/includes/media.php | Outputs the legacy media upload form for the media library. |
| has_meta() wp-admin/includes/post.php | Returns meta data for the given post ID. |
| bulk_edit_posts() wp-admin/includes/post.php | Processes the post data for the bulk editing of posts. |
| WP_Importer::get_imported_comments() wp-admin/includes/class-wp-importer.php | Set array with imported comments from WordPress database |
| WP_Importer::get_imported_posts() wp-admin/includes/class-wp-importer.php | Returns array with imported permalinks from WordPress database |
| WP_Importer::count_imported_posts() wp-admin/includes/class-wp-importer.php | Return count of imported permalinks from WordPress database |
| get_pending_comments_num() wp-admin/includes/comment.php | Gets the number of pending comments on a post or posts. |
| cache_users() wp-includes/pluggable.php | Retrieves info for user lists to prevent multiple queries by get_userdata() . |
| wp_get_archives() wp-includes/general-template.php | Displays archive links based on type and format. |
| get_calendar() wp-includes/general-template.php | Displays calendar with days that have posts as links. |
| get_users_of_blog() wp-includes/deprecated.php | Get users for the site. |
| WP_Query::get_posts() wp-includes/class-wp-query.php | Retrieves an array of posts based on query variables. |
| wp_scheduled_delete() wp-includes/functions.php | Permanently deletes comments or posts of any type that have held a status of ‘trash’ for the number of days defined in EMPTY_TRASH_DAYS. |
| is_blog_installed() wp-includes/functions.php | Determines whether WordPress is already installed. |
| _pad_term_counts() wp-includes/taxonomy.php | Adds count of children to parent count. |
| clean_term_cache() wp-includes/taxonomy.php | Removes all of the term IDs from the cache. |
| wp_delete_term() wp-includes/taxonomy.php | Removes a term from the database. |
| wp_load_alloptions() wp-includes/option.php | Loads and caches all autoloaded options, if available or all options. |
| wp_load_core_site_options() wp-includes/option.php | Loads and caches certain often requested site options if is_multisite() and a persistent cache is not being used. |
| WP_User_Query::query() wp-includes/class-wp-user-query.php | Executes the query, with the current variables. |
| count_many_users_posts() wp-includes/user.php | Gets the number of posts written by a list of users. |
| wp_enqueue_media() wp-includes/media.php | Enqueues all scripts, styles, settings, and templates necessary to use all media JS APIs. |
| _prime_post_caches() wp-includes/post.php | Adds any posts from the given IDs to the cache that do not already exist in cache. |
| get_page_by_path() wp-includes/post.php | Retrieves a page given its path. |
| get_pages() wp-includes/post.php | Retrieves an array of pages (or hierarchical post type items). |
| wp_delete_post() wp-includes/post.php | Trashes or deletes a post or page. |
| wp_trash_post_comments() wp-includes/post.php | Moves comments for a post to the Trash. |
| wp_count_posts() wp-includes/post.php | Counts number of posts of a post type and if user has permissions to view. |
| wp_count_attachments() wp-includes/post.php | Counts number of attachments for the mime type(s). |
| WP_Rewrite::page_uri_index() wp-includes/class-wp-rewrite.php | Retrieves all pages and attachments for pages URIs. |
| redirect_canonical() wp-includes/canonical.php | Redirects incoming links to the proper URL based on the site url. |
| wp_get_post_autosave() wp-includes/revision.php | Retrieves the autosaved data of the specified post. |
| install_blog() wp-includes/ms-deprecated.php | Install an empty blog. |
| get_admin_users_for_domain() wp-includes/ms-deprecated.php | Get the admin for a domain/path combination. |
| get_bookmarks() wp-includes/bookmark.php | Retrieves the list of bookmarks. |
| get_blog_list() wp-includes/ms-deprecated.php | Deprecated functionality to retrieve a list of all sites. |
| wp_list_authors() wp-includes/author-template.php | Lists all the authors of the site, with several options available. |
| get_last_updated() wp-includes/ms-blogs.php | Get a list of most recently updated blogs. |
| wp_xmlrpc_server::mt_getTrackbackPings() wp-includes/class-wp-xmlrpc-server.php | Retrieve trackbacks sent to a given post. |
| wp_xmlrpc_server::pingback_ping() wp-includes/class-wp-xmlrpc-server.php | Retrieves a pingback and registers it. |
| wp_xmlrpc_server::pingback_extensions_getPingbacks() wp-includes/class-wp-xmlrpc-server.php | Retrieve array of URLs that pingbacked the given URL. |
| wp_xmlrpc_server::attach_uploads() wp-includes/class-wp-xmlrpc-server.php | Attach upload to a post. |
| wp_xmlrpc_server::wp_getPageList() wp-includes/class-wp-xmlrpc-server.php | Retrieve page list. |
| update_meta_cache() wp-includes/meta.php | Updates the metadata cache for the specified objects. |
Changelog
| Version | Description |
|---|---|
| 0.71 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wpdb/get_results