0

I got a metabox that hold a monthly budget per client...
that meta box is inside a custom post type called clients. Inside that metabox the only thing entered is numbers.

I want to get the sum of all the clients.
Any ideas?

Altough i think its pure php.. somehow using foreach - to the request of @Fahd Murtaza i am trying to add parts of my metabox... i am using alchemy as the base to create my meta boxes so would add some parts.. Hope this doesnt confuse more..

<?php
// structure of my metabox usign alchemy
$clients = new WPAlchemy_MetaBox(array
(
    'id' => '_project_description',
    'title' => __('Client Info Card', 'sagive'),
    'template' => get_stylesheet_directory() . '/metaboxes/clients-meta.php',
    'types' => array('portfolio'),
    'context' => 'normal',
    'priority' => 'high',
    'autosave' => TRUE

));

.

// call for budget section in my meta box
<p>
<label><?php _e('Clients Budget:', 'sagive'); ?></label>
    <?php $mb->the_field('client_budget'); ?>
    <input type="text" name="<?php $metabox->the_name('client_budget'); ?>" value="<?php $metabox->the_value('client_budget'); ?>"/>
</p>  

.
The rest of the code is way to long and i think not relevant to show... correct me if i am wrong and would add it via pastebin or similer..

.
THIS IS HOW I QUERY THE POSTS...

<?php 
    query_posts(array( 
        'post_type' => 'portfolio',
        'showposts' => 10 
    ) );                    
    $count_posts_portfolio = wp_count_posts( 'portfolio' )->publish;
?>
2
  • Please share your metabox code.
    – Fahad
    Jan 7, 2012 at 21:52
  • my meta boxes are set using alchemy script so.. they are spread on many files.. i tried adding them to the question as organized as i can..
    – Sagive
    Jan 7, 2012 at 22:32

2 Answers 2

1

Asuming that you have a variable called $posts that contains all the posts (or easier: Post IDs) from some query.

You can then work with $result and process everything you need. Calling post data is easy, as the key would be the post ID.

// For other arguments look into @link http://codex.wordpress.org/Class_Reference/WP_Query
$posts = new WP_Query( 
     'post_type' => 'portfolio'
    ,'post_status' => 'publish'
);

foreach ( $posts as $post )
{
    $result[ $post->ID ]['post_data'] = $post->ID;
    $result[ $post->ID ]['meta_data'] = get_post_custom( $post->ID );
}

foreach( $result as $id => $data )
{
    // Do whatever you need to do with your meta data in here.
    print 'The title is: '.$post->post_title;

    // See your post data:
    print_r( $data['post_data'] );

    // See your meta data:
    print_r( $data['meta_data'] );
}
3
  • Sorry @kaiser.. i dont where to use that and how to turn $result[ $post->ID ] into a sum... i have added to my question the query i currently use... can share a more descriptive answer? :)
    – Sagive
    Jan 8, 2012 at 23:07
  • See update. But honestly: You can check about a hundred Qs here in WPSE to see how you access meta data. The same goes for WP Codex. And calculating the values from the meta data is basic php, non-WP stuff you can read about on php.net.
    – kaiser
    Jan 9, 2012 at 0:25
  • i can do the calculating no prob.. my problem was how to inside the loop... thanks man.. going to try your answer
    – Sagive
    Jan 9, 2012 at 1:14
0
$args = array(
    'numberposts' => -1,  // all the posts
    'post_type'   => 'clients'
);
$clients = get_posts( $args );

$total = 0;
foreach( $clients as $client ) {
    $single = get_post_meta( $client->ID, 'metakey_name_here', true );
    $total += $single;
}

$total should now contain the total for all the clients. metakey_name_here is going to be whatever $metabox->the_name( 'client_budget' ) is, I presume that is set elsewhere, but I've never worked with the WP_Alchemy class, so I can't tell you definitively. If you need more functionality than get_posts() offers, WP_Query is gonna be the place I would look. Also, here's documentation on get_post_meta() in case you need it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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