wpdb::get_var()publicWP 0.71

Retrieves one variable from the database.

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.

Method of the class: wpdb{}

No Hooks.

Return

String|null. Database query result (as string), or null on failure.

Usage

global $wpdb;
$wpdb->get_var( $query, $x, $y );
$query(string|null)
SQL query.
Default: null, use the result from the previous query
$x(int)
Column of value to return. Indexed from 0.
$y(int)
Row of value to return. Indexed from 0.

Examples

0

#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>';
0

#2 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>';
0

#3 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;
}

Changelog

Since 0.71 Introduced.

wpdb::get_var() code WP 6.4.3

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;
}