WordPress.org

Forums

Calculation in "add_post_meta" (2 posts)

  1. themax
    Member
    Posted 2 years ago #

    Hello. I have a question.
    I have 3 custom-fileds, for example is "gain", "spent" and "total".
    I want to fill "total" automatically, like "gain - spent = total".

    Function "get_post_meta" works not correctly in that case, cause it get meta-value after I click "publicate" and I need click "update" again.

    I need some help. How can I got value in custom-field "total" from other meta-values.

    Sry for my English. =)

  2. webmystery
    Member
    Posted 2 years ago #

    I'm going to be trying something like this soon - was checking to see if some else had already solved it.

    I tink that "Total" will not be a custom field. It will be echoed back to the screen in the front-end view - yes, after the page is updated.
    In a custom page template I'd try something like this:

    <?php
    $gain=(get_post_meta($post->ID, 'gain', true));
    $spent=(get_post_meta($post->ID, 'spent', true));
    $total=($gain-$spent);?>
    <p>The total remaining  balance is: $<?php echo($total);?></p>

    Or you could just echo back <?php echo($gain-$spent);?> if you don't need the $total figure elsewhere. Someone more proficient in php could doubtless write a more elegant version of this.

Topic Closed

This topic has been closed to new replies.

About this Topic