【问题标题】:Wordpress - Add numerical values of custom fields togetherWordpress - 将自定义字段的数值添加在一起
【发布时间】:2012-12-18 23:08:46
【问题描述】:

我在我的 WordPress 博客中创建了一个名为“评论”的自定义帖子类型。在这里面我有一个自定义字段,其数字为 5。

我正在尝试将所有这些数字相加,然后除以帖子的数量以获得总分。

我正在尝试使用此代码...

<?php
$total_score=0;
$meta_key = 'target';//set this to your custom field meta key
$allscores=$wpdb->get_col($wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE     meta_key = %s", $meta_key));
foreach ($allscores as $score) {
$total_score = $total_score + $score;
}
echo 'Total score is '.$total_score;
?>

但是我得到的结果是 0,谁能看出我哪里出错了?

【问题讨论】:

  • 在循环访问 $allscores 时尝试回显 $score 变量。这可能会揭示问题。

标签: php wordpress aggregate custom-post-type


【解决方案1】:

原来一直是我,输入数据库的数字是按字母顺序拼写的,而不是数字,所以我收到的 0 结果在技术上是正确的 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    相关资源
    最近更新 更多