我有一个foreach,用于按术语在一个分类过滤器中显示所有帖子
我有一个用于显示数值的自定义字段<我需要的是对该字段的所有值进行两次数学运算,跨越该学期的所有帖子。
我的意思是:
分类法X有4个术语<每个学期有4个职位
每篇文章都有一个自定义\\u字段
我需要将该术语中的所有custom\\u字段值与该自定义字段的平均值相加。
因此,目前我有:
<?php foreach ( $prod_terms as $prod_term ) {
$prod_query = new WP_Query( array(
\'post_type\' => \'prod_cientifica\',
\'prod_area\' => $post_slug,
\'tax_query\' => array(
array(
\'taxonomy\' => \'prod_tipo\',
\'terms\' => array( $prod_term->slug ),
\'operator\' => \'IN\',
\'get\' => \'all\',
\'field\' => \'slug\'
)
)
) );
if ( $prod_query->have_posts() ) : while ( $prod_query->have_posts() ) : $prod_query->the_post(); ?>
<div class="areaItem">
<h4><?php the_title(); ?></h4>
<p class="autores"><?php the_field(\'prod_autores\'); ?></p>
<p class="info"><?php the_field(\'prod_info\'); ?></p>
<p class="info"><?php the_field(\'prod_fi\'); ?></p>
</div>
<?php endwhile; endif; ?>
我需要的是把所有的
prod_fi 每个学期的字段。此外,该字段的平均值也是。
有什么想法吗?
谢谢
--使用代码编辑。
到目前为止,我尝试了几种选择。更接近的是:
<?php
if ( $prod_query->have_posts() ) : while ( $prod_query->have_posts() ) : $prod_query->the_post();
$total_fi = array();
$meta_key = \'prod_fi\';//set this to your custom field meta key
$total_fi = $wpdb->get_col($wpdb->prepare
("SELECT meta_value FROM wp_postmeta as pm
INNER JOIN wp_term_relationships as tr ON (pm.post_id = tr.object_id)
INNER JOIN wp_term_taxonomy as tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
WHERE 1
AND tt.taxonomy = \'prod_tipo\'
AND pm.meta_key = %s",
$meta_key));
echo \'Total FI \'.array_sum( $total_fi );
?>
但是,这一个显示了分类法中所有custom\\u字段的总数量。
这一个,并不是每一项的和的停止,它继续。
$factorI = get_field(\'prod_fi\');
if($factorI) {$factor_total += $factorI;}
echo $factor_total;
谢谢你。
我找到了一个棘手的解决办法。我知道这不是正确的方法,如果有人知道正确的方法,我会很高兴的!!!!
<?php
//start the foreach for each term in taxonomy
foreach ( $prod_terms as $prod_term ) {
$prod_query = new WP_Query( array(
\'post_type\' => \'prod_cientifica\',
\'prod_area\' => $post_slug,
\'tax_query\' => array(
array(
\'taxonomy\' => \'prod_tipo\',
\'terms\' => array( $prod_term->slug ),
\'operator\' => \'IN\',
\'get\' => \'all\',
\'field\' => \'slug\'
)
)
) );
?>
<div class="area">
<h2><?php echo $prod_term->name; ?></h2>
<?php
// First while for fetch the value of "prod_fi" and make the math.
while ( $prod_query->have_posts() ) : $prod_query->the_post();
$factorI = get_field(\'prod_fi\');
if($factorI) {$factor_total += $factorI;}
// count post for average math operation
$count_posts = $prod_query->current_post + 1;
$factorP = $factor_total / $count_posts;
endwhile; ?>
<p class="areasPublicados"><?php _e(\'Publicados: \', \'twentytwelve\'); ?> <?php echo $count_posts; ?></p>
<p class="areasFI fleft"><?php _e(\'FI medio: \', \'twentytwelve\'); ?> <?php echo $factorP; ?> </p><p class="areasFI fright"><?php _e(\'FI: \', \'twentytwelve\'); ?> <?php echo $factor_total; ?></p>
<?php
//original query to fetch the post in the term
if ( $prod_query->have_posts() ) : while ( $prod_query->have_posts() ) : $prod_query->the_post(); ?>
<div class="areaItem">
<h4><?php the_title(); ?></h4>
<p class="autores"><?php the_field(\'prod_autores\'); ?></p>
<p class="info"><?php the_field(\'prod_info\'); ?></p>
</div>
<?php endwhile; endif; ?>
</div>
<?php
// Reset math operation of factor_total, so it don\'t continue sum
$factor_total = null;
$prod_query = null;
wp_reset_postdata();
} //endforeach
?>
再次编辑!
<?php
// Fetch all post in taxonomy divided by terms.
foreach ( $prod_terms as $prod_term ) {
$prod_query = new WP_Query( array(
\'post_type\' => \'prod_cientifica\',
\'prod_area\' => $post_slug,
\'tax_query\' => array(
array(
\'taxonomy\' => \'prod_tipo\',
\'terms\' => array( $prod_term->slug ),
\'operator\' => \'IN\',
\'get\' => \'all\',
\'field\' => \'slug\'
)
)
) );
?>
<?php if($prod_query->have_posts()) : ?>
<div class="area">
<h2><?php echo $prod_term->name; ?></h2>
<?php
// Declare total and count before loop
$factorTotal = 0;
$factorCount = 0;
// First loop to fetch prod_fi value
while ( $prod_query->have_posts() ) : $prod_query->the_post();
$factorI = get_field(\'prod_fi\');
if($factorI) { // Only if it exists
$factorTotal += $factorI; // Add it
$factorCount++; // Count it
}
endwhile; ?>
<p class="areasPublicados">
<?php _e(\'Publicados: \', \'twentytwelve\'); ?> <?php echo $factorCount; ?>
</p>
<p class="areasFI fleft">
<?php _e(\'FI medio: \', \'twentytwelve\'); ?> <?php echo $factorTotal; ?>
</p>
<p class="areasFI fright">
<?php _e(\'FI: \', \'twentytwelve\'); ?> <?php echo ($factorTotal / $factorCount); ?>
</p>
<?php
if ( $prod_query->have_posts() ) : while ( $prod_query->have_posts() ) : $prod_query->the_post(); ?>
<div class="areaItem">
<h4><?php the_title(); ?></h4>
<p class="autores"><?php the_field(\'prod_autores\'); ?></p>
<p class="info"><?php the_field(\'prod_info\'); ?></p>
</div>
<?php endwhile; endif; ?>
</div>
<?php endif; ?>
<?php
// Reset things, for good measure
$factor_total = null;
$prod_query = null;
wp_reset_postdata();
} //enforeach
?>