需要计算wordpress帖子的元值数量

In this script i need to replace the $count variable with the number of post counts from the wordpress database:

<?php               
$state_query = $wpdb->get_results("
SELECT meta_value AS state, COUNT(post_id) AS count
FROM {$wpdb->postmeta} WHERE meta_key = 'state'
GROUP BY state ORDER BY count DESC"); 

$count = 50; //here i need to count the number of meta value of each post
$max = 100;
$scale = 1.0;

if ( !empty($max) ) { $percent = ($count * 100) / $max; } 
else { $percent = 0; }
?>

<?php if ($state_query) {
echo '<div class="cf">'; ?>
<?php foreach ($state_query as $st) 
echo ($percent * $scale);
?>              
<?php echo '</div>'; } ?>

Try

$count = count($state_query);

you can use num_rows like this

 $count = $wpdb->num_rows;

Declaring $wpdb as global and using it to execute an SQL query statement that returns a PHP object

May try this will help you lot.

$state_query = $wpdb->get_results("
SELECT meta_value AS state, COUNT(post_id) AS count
FROM {$wpdb->postmeta} WHERE meta_key = 'state'
GROUP BY state ORDER BY count DESC", OBJECT ); 


if($wpdb->num_rows)
{
    foreach ($state_query  as $result)
    {
    $count = $result->count;
    $max = 100;
    $scale = 1.0;
    }
}else{
    $count = 50; //here i need to count the number of meta value of each post
    $max = 100;
    $scale = 1.0;
}