How to count data records in wordpress which have same meta_value in wp_postmeta table?

I want to get number or count of same meta_value of meta_key == ‘parent_id’.How can i get it?
What should be the wordpress query for counting records in same table?

enter image description here

//set the meta_key to the appropriate custom field meta key
$meta_key = 'parent_id';

$allmiles = $wpdb->get_var( $wpdb->prepare( 
  "SELECT meta_value,
  SUM(IF(meta_value = "Purva", 1,0)) AS 'parent_id',
  COUNT(meta_value) AS 'total'
  FROM $wpdb->wp_postmeta
  GROUP BY post_id
  ORDER BY parent_id DESC", 
  $meta_key
) );

echo "<p>Total miles is {$allmiles}</p>"; 

1 Answer
1

<?php 
   global $wpdb;
   $table_name = $wpdb->prefix . "postmeta"; 
   $login_name = sanitize_user( $_POST['parent_id'] ); 
   $prepare = $wpdb->prepare( "SELECT COUNT(*) FROM $table_name WHERE    meta_value="Purva" AND meta_key = 'parent_id' ", $login_name );
   $myrows = $wpdb->get_results( $prepare );
   echo '<pre>' . print_r( $myrows, true ) . '</pre>';
?>

I tried this and get values but still something missing…

Leave a Comment