如何计算wp_postmeta表中具有相同meta_value的WordPress中的数据记录?

时间:2016-08-23 作者:P. Sheth

我想获取meta\\u key==“parent\\u id”的相同meta\\u值的数目或计数。我怎样才能得到它?wordpress查询同一个表中的记录应该是什么?

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 个回复
SO网友:P. Sheth

<?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>\';
?>
我尝试了这个,得到了一些值,但仍然缺少一些东西。。。