从自定义帖子字段中总结一个specyfic meta_value

时间:2018-06-12 08:15:46

标签: wordpress

我正在尝试总结一个特定的meta_value资金。在我的meta_value单元格中,有自定义帖子字段。一个是资金,另一个是自己的贡献。

我已尝试过codex页面中的示例,但它不起作用,因为当我回显$sumFunding变量时,我得到一个0。

  $meta_key = 'investments_details';
  $sumFunding = $wpdb->get_var( $wpdb->prepare(
  "
    SELECT sum(meta_value)
    FROM $wpdb->postmeta
    WHERE meta_key = %s", $meta_key) );

如何让这个工作?

1 个答案:

答案 0 :(得分:1)

您提供的代码并不包含您编写的元字段。

试试这段代码:

 int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);

    Uri defaultSoundUri;
    defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    //}
    Bitmap largeIcon = BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher);

    RemoteViews expandedView = new RemoteViews(getActivity().getPackageName(), R.layout.view_schedule_notification);
    expandedView.setTextViewText(R.id.timestamp, DateUtils.formatDateTime(getActivity(), System.currentTimeMillis(),
            DateUtils.FORMAT_SHOW_TIME));
    expandedView.setTextViewText(R.id.content_text, msg);
    RemoteViews collapsedView = new RemoteViews(getActivity().getPackageName(), R.layout.view_collapsed_notification);
    collapsedView.setTextViewText(R.id.timestamp, DateUtils.formatDateTime(getActivity(), System.currentTimeMillis(),
            DateUtils.FORMAT_SHOW_TIME));
    collapsedView.setTextViewText(R.id.content_text, msg);

    Intent acceptIntent = new Intent(getActivity(), VersionTwoOrderHistorySummary.class);
    acceptIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pIntentAccept = PendingIntent.getActivity(getActivity(), iUniqueId, acceptIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    expandedView.setOnClickPendingIntent(R.id.btn_schedule_accept, pIntentAccept);
    expandedView.setOnClickPendingIntent(R.id.btn_schedule_change, pIntentAccept);
    expandedView.setOnClickPendingIntent(R.id.btn_schedule_reject, pIntentAccept);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(getActivity())
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(getResources().getString(R.string.app_name))
            .setAutoCancel(true)
            .setLargeIcon(largeIcon)
            .setContentText(msg)
            .setContentIntent(pIntentAccept)
            .setTicker(getResources().getString(R.string.app_name))
            .setCustomContentView(collapsedView)
            .setCustomBigContentView(expandedView)
            .setSound(defaultSoundUri);
    //.setStyle(new android.support.v7.app.NotificationCompat.DecoratedCustomViewStyle());


    NotificationManager notificationManager =
            (NotificationManager) getActivity().getSystemService(NOTIFICATION_SERVICE);
    Random rand = new Random();
    int code = rand.nextInt();

    builder.build().flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(code, builder.build());

如果你想通过给定的帖子,

$meta_key_1 = 'funding';
$meta_key_2='ownContribution';
$sumFunding = $wpdb->get_var( $wpdb->prepare(
"SELECT sum(meta_value)
 FROM $wpdb->postmeta
 WHERE meta_key = %s or meta_key = %s", $meta_key_1,$meta_key_2) );

更新: 由于数据值在序列化数据中,因此需要采用不同的方法:

$meta_key_1 = 'funding';
$meta_key_2='ownContribution';
$post_id=SOME_ID_HERE;
$sumFunding = $wpdb->get_var( $wpdb->prepare(
"SELECT sum(meta_value)
 FROM $wpdb->postmeta
 WHERE post_id=%d and (meta_key = %s or meta_key = %s)",$post_id, $meta_key_1,$meta_key_2) );

UPDATE2: 循环所有帖子

 $post_id=SOME_ID_HERE;
 $investments_details=get_post_meta($post_id,'investments_details',true);
 $investments_details_sum=$investments_details['funding']+$investments_details['ownContribution'];