我需要得到meta\\u键不同值的计数:
假设我有meta\\U密钥$class
&;只有三个不同的值:1
, 2
&;3
.
//My WP_Query
$ads = new WP_Query(array(
\'post_type\' => \'ads\',
\'meta_key\' => \'ads_class\',
\'orderby\' => \'meta_value_num\'
));
if ( $ads->have_posts() ) :
//Do stuff
//Now I need to know how many have ads_class of "1", "2" and "3" - all as different vars
最合适的回答,由SO网友:TheDeadMedic 整理而成
只需在帖子上循环,并根据ads_class
:
$ads_1 =
$ads_2 =
$ads_3 = 0;
while ( $ads->have_post() ) {
$ads->the_post();
switch ( ( int ) get_post()->ads_class ) {
case 1 :
$ads_1++;
break;
case 2 :
$ads_2++;
break;
case 3 :
$ads_3++;
break;
}
}
$ads_1; // Number of "1" ads
$ads_2; // Number of "2" ads
$ads_3; // Number of "3" ads