0

I am trying to add the values of a variable together and display the total value.

I am using posts to posts to create a connection between my custom post types 'recipes' and 'ingredients'. the ingredients post type has a custom meta field with the key 'foxware-ingredient-cost'. As you can see in the code below i assign the value from the meta key to the variable $cost. Then i am trying to add each of those values together, but the code i have doesn't seem to do anything except return the first value, the plus sign seems to have no effect here. Do i need to put the values of $cost variable into an array or something?

<?php

    $connected = new WP_Query( array(
    'connected_type' => 'recipes_to_ingredients',
    'connected_items' => get_queried_object(),

) );

while( $connected->have_posts() ) : $connected->the_post();

    $cost = get_post_meta( $post->ID, 'foxware-ingredient-cost', true );

        echo '<li>';

        the_title();

        // Display cost
        echo '<br>';
         echo 'Cost: ' . $cost;
        echo '</li>';
endwhile;

        echo ($cost + $cost); ?>
2
  • Please contact the plugin author for support. Third part plugins are off topic here. Please see How to Ask and help center – Pieter Goosen Dec 22 '14 at 14:23
  • You are wrong assuming that the while loop returns a value of $cost and you are wrong asumming that each loop itinerancy will store diferent instances of the $cost. You problem is a basic understanding of PHP programing. You can get better help in StackOverflow. – cybmeta Dec 22 '14 at 15:33
1

Try to change with this:

    ........
    ........
    ........


    $fullcost = 0;
    while( $connected->have_posts() ) : $connected->the_post();

        $cost = get_post_meta( $post->ID, 'foxware-ingredient-cost', true );
        $fullcost += $cost;
        echo '<li>';

        the_title();

        // Display cost
        echo '<br>';
         echo 'Cost: ' . $cost;
        echo '</li>';
    endwhile;

    echo $fullcost; ?>
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.