On this page
wpdb::get_var( string|null $query = null, int $x, int $y ): string|null
Retrieves one variable from the database.
Description
Executes a SQL query and returns the value from the SQL result.
If the SQL result contains more than one column and/or more than one row, the value in the column and row specified is returned. If $query is null, the value in the specified column and row from the previous SQL result is returned.
Parameters
$querystring|null Optional-
SQL query. Defaults to null, use the result from the previous query.
Default:
null $xint Optional-
Column of value to return. Indexed from 0.
$yint Optional-
Row of value to return. Indexed from 0.
Return
string|null Database query result (as string), or null on failure.
Source
File: wp-includes/class-wpdb.php. View all references
public function get_var( $query = null, $x = 0, $y = 0 ) {
$this->func_call = "\$db->get_var(\"$query\", $x, $y)";
if ( $query ) {
if ( $this->check_current_query && $this->check_safe_collation( $query ) ) {
$this->check_current_query = false;
}
$this->query( $query );
}
// Extract var out of cached results based on x,y vals.
if ( ! empty( $this->last_result[ $y ] ) ) {
$values = array_values( get_object_vars( $this->last_result[ $y ] ) );
}
// If there is a value return it, else return null.
return ( isset( $values[ $x ] ) && '' !== $values[ $x ] ) ? $values[ $x ] : 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_update_user_counts() wp-includes/user.php | Updates the total count of users on the site. |
| _wp_batch_update_comment_type() wp-includes/comment.php | Updates the comment type for a batch of comments. |
| WP_Debug_Data::debug_data() wp-admin/includes/class-wp-debug-data.php | Static function for generating site debug data when required. |
| WP_Site_Health::prepare_sql_data() wp-admin/includes/class-wp-site-health.php | Runs the SQL version checks. |
| is_site_meta_supported() wp-includes/functions.php | Determines whether site meta is enabled. |
| _find_post_by_old_slug() wp-includes/query.php | Find the post ID for redirecting an old slug. |
| _find_post_by_old_date() wp-includes/query.php | Find the post ID for redirecting an old date. |
| wp_check_comment_flood() wp-includes/comment.php | Checks whether comment flooding is occurring. |
| WP_Term_Query::get_terms() wp-includes/class-wp-term-query.php | Retrieves the query results. |
| WP_Network_Query::set_found_networks() wp-includes/class-wp-network-query.php | Populates found_networks and max_num_pages properties for the current query if the limit clause was used. |
| WP_Network_Query::get_network_ids() wp-includes/class-wp-network-query.php | Used internally to get a list of network IDs matching the query vars. |
| WP_Comment_Query::set_found_comments() wp-includes/class-wp-comment-query.php | Populates found_comments and max_num_pages properties for the current query if the limit clause was used. |
| WP_Site_Query::get_site_ids() wp-includes/class-wp-site-query.php | Used internally to get a list of site IDs matching the query vars. |
| WP_Site_Query::set_found_sites() wp-includes/class-wp-site-query.php | Populates found_sites and max_num_pages properties for the current query if the limit clause was used. |
| wp_term_is_shared() wp-includes/taxonomy.php | Determines whether a term is shared between multiple taxonomies. |
| WP_Comment_Query::get_comment_ids() wp-includes/class-wp-comment-query.php | Used internally to get a list of comment IDs matching the query vars. |
| network_step2() wp-admin/includes/network.php | Prints step 2 for Network installation process. |
| display_setup_form() wp-admin/install.php | Displays installer setup form. |
| network_domain_check() wp-admin/includes/network.php | Check for an existing network. |
| WP_User_Search::query() wp-admin/includes/deprecated.php | Executes the user search query. |
| populate_network() wp-admin/includes/schema.php | Populate network settings. |
| maybe_disable_link_manager() wp-admin/includes/upgrade.php | Disables the Link Manager on upgrade if, at the time of upgrade, no links exist in the DB. |
| pre_schema_upgrade() wp-admin/includes/upgrade.php | Runs before the schema is upgraded. |
| maybe_create_table() wp-admin/includes/upgrade.php | Creates a table in the database, if it doesn’t already exist. |
| upgrade_network() wp-admin/includes/upgrade.php | Executes network-level upgrade routines. |
| update_gallery_tab() wp-admin/includes/media.php | Adds the gallery tab back to the tabs array if post has image attachments. |
| post_exists() wp-admin/includes/post.php | Determines if a post exists based on title, content, date and type. |
| comment_exists() wp-admin/includes/comment.php | Determines if a comment exists based on author and date. |
| WP_Posts_List_Table::__construct() wp-admin/includes/class-wp-posts-list-table.php | Constructor. |
| wp_notify_moderator() wp-includes/pluggable.php | Notifies the moderator of the site about a new comment that is awaiting approval. |
| get_calendar() wp-includes/general-template.php | Displays calendar with days that have posts as links. |
| WP_Query::set_found_posts() wp-includes/class-wp-query.php | Set up the amount of found posts and the number of pages (if limit clause was used) for the current query. |
| is_blog_installed() wp-includes/functions.php | Determines whether WordPress is already installed. |
| do_enclose() wp-includes/functions.php | Checks content for video and audio links to add as enclosures. |
| _update_post_term_count() wp-includes/taxonomy.php | Updates term count based on object types of the current taxonomy. |
| _update_generic_term_count() wp-includes/taxonomy.php | Updates term count based on number of objects. |
| wp_unique_term_slug() wp-includes/taxonomy.php | Makes term slug unique, if it isn’t already. |
| wp_update_term() wp-includes/taxonomy.php | Updates term based on arguments provided. |
| wp_insert_term() wp-includes/taxonomy.php | Adds a new term to the database. |
| wp_set_object_terms() wp-includes/taxonomy.php | Creates term and taxonomy relationships. |
| wp_delete_term() wp-includes/taxonomy.php | Removes a term from the database. |
| get_adjacent_post() wp-includes/link-template.php | Retrieves the adjacent post. |
| ms_allowed_http_request_hosts() wp-includes/http.php | Adds any domain in a multisite installation for safe HTTP requests to the allowed list. |
| WP_User_Query::query() wp-includes/class-wp-user-query.php | Executes the query, with the current variables. |
| wp_insert_user() wp-includes/user.php | Inserts a user into the database. |
| count_user_posts() wp-includes/user.php | Gets the number of posts a user has written. |
| wp_enqueue_media() wp-includes/media.php | Enqueues all scripts, styles, settings, and templates necessary to use all media JS APIs. |
| _get_last_post_time() wp-includes/post.php | Gets the timestamp of the last time any post was modified or published. |
| wp_unique_post_slug() wp-includes/post.php | Computes a unique slug for the post, when given the desired slug and some post details. |
| wp_insert_post() wp-includes/post.php | Inserts or update a post. |
| wp_count_attachments() wp-includes/post.php | Counts number of attachments for the mime type(s). |
| redirect_canonical() wp-includes/canonical.php | Redirects incoming links to the proper URL based on the site url. |
| redirect_guess_404_permalink() wp-includes/canonical.php | Attempts to guess the correct URL for a 404 request based on query vars. |
| update_posts_count() wp-includes/ms-functions.php | Updates a blog’s post count. |
| wpmu_validate_blog_signup() wp-includes/ms-functions.php | Processes new site registrations. |
| ms_not_installed() wp-includes/ms-load.php | Displays a failure message. |
| get_blog_list() wp-includes/ms-deprecated.php | Deprecated functionality to retrieve a list of all sites. |
| get_blog_status() wp-includes/ms-blogs.php | Get a blog details field. |
| wp_xmlrpc_server::pingback_ping() wp-includes/class-wp-xmlrpc-server.php | Retrieves a pingback and registers it. |
| wpdb::get_col() wp-includes/class-wpdb.php | Retrieves one column from the database. |
| wp_update_comment_count_now() wp-includes/comment.php | Updates the comment count for the post. |
| get_lastcommentmodified() wp-includes/comment.php | Retrieves the date the last comment was modified. |
| wp_allow_comment() wp-includes/comment.php | Validates whether this comment is allowed to be made. |
| check_comment() wp-includes/comment.php | Checks whether a comment passes internal checks to be allowed to add. |
| add_metadata() wp-includes/meta.php | Adds metadata for the specified object. |
Changelog
| Version | Description |
|---|---|
| 0.71 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wpdb/get_var