wpdb::get_var()
Gets the value of a single cell from the query result. By default, the first cell is taken — this is the first column and the first row.
If the query result contains more than one column and/or more than one row, the value of the specified cell will be returned (specified in the 2nd and 3rd parameters). Thus, to get the value of the second cell from the second row of the query result, you need to specify the second and third parameters: $column_offset = 1, $row_offset = 1.
If you call the method without a query: $query = null, the value of the specified cell from the previous query will be returned.
Method of the class: wpdb{}
No Hooks.
Returns
String|null. The value of the database table cell as a string.
cell value— if the query retrieves the value of a single cell (column in a row).value of the first cell of the first column— if the query retrieves one row and multiple columns.value of the first cell of the first row and column— if the query retrieves multiple rows and columns.NULL— when there is no result.
Usage
global $wpdb; $wpdb->get_var( $query, $x, $y );
- $query(string/null)
- The query to execute. You can set this parameter to null, then the function will return the result of the last query that was executed by the class (stored in the variable).
- $x(number)
- The desired column (column offset). By default 0 - first column.
- $y(number)
- The desired row (row offset). By default 0 - first row.
Examples
#1 Display the number of users
$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users;" ); echo '<p>The number of users is: ' . $user_count . '</p>';
#2 A set of blog statistics outputs
# Total Number of authors
function get_totalauthors() {
global $wpdb;
$totalauthors = intval( $wpdb->get_var(
"
SELECT COUNT(ID) FROM $wpdb->users
LEFT JOIN $wpdb->usermeta ON $wpdb->usermeta.user_id = $wpdb->users.ID
WHERE $wpdb->users.user_activation_key = '' AND $wpdb->usermeta.meta_key = '{$wpdb->prefix}user_level' AND (meta_value+0.00) > 1
"
) );
return $totalauthors;
}
# Total Number of posts
function get_totalposts(){
global $wpdb;
$totalposts = intval( $wpdb->get_var(
"SELECT COUNT(ID) FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish'"
) );
return $totalposts;
}
# Total Number of Pages
function get_totalpages() {
global $wpdb;
$totalpages = intval( $wpdb->get_var(
"SELECT COUNT(ID) FROM $wpdb->posts WHERE post_type = 'page' AND post_status = 'publish'"
) );
return $totalpages;
}
# Total Number of Comments
function get_totalcomments() {
global $wpdb;
$totalcomments = intval( $wpdb->get_var(
"SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = '1'"
) );
return $totalcomments;
}
# Total Number of Commentators
function get_totalcommentposters() {
global $wpdb;
$totalcommentposters = intval($wpdb->get_var(
"SELECT COUNT(DISTINCT comment_author) FROM $wpdb->comments WHERE comment_approved = '1' AND comment_type = ''"
) );
return $totalcommentposters;
}
# Total Number of links
function get_totallinks() {
global $wpdb;
$totallinks = intval( $wpdb->get_var("SELECT COUNT(link_id) FROM $wpdb->links") );
return $totallinks;
} #3 Display the sum of the values of the defined custom fields
// define the custom key to be counted $meta_key = 'miles'; $allmiles = $wpdb->get_var( $wpdb->prepare( "SELECT sum(meta_value) FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key ) ); echo '<p>Total number of custom fields: '. $allmiles . '</p>';
Changelog
| Since 0.71 | Introduced. |
wpdb::get_var() wpdb::get var code WP 6.9.1
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;
}