Calculate multiples $var on get_post_meta

221 views Asked by At

I'm new to PHP and appreciate any help. I'm trying to understand why $skill is not working on get_post_meta on Wordpress.

I'm trying to calculate (sum) all meta fields numbers in each meta fields = 'ecpt_editorial', 'ecpt_branding', etc. (i.e. = 'ecpt_editoral' has 3 points in one post + 4 points in another one.) I'm trying to calculate them all without need to create a $ to each of them (they're too many).

My errors:
Warning: Illegal offset type in isset or empty in
Fatal error: Unsupported operand types in

 <?php $args = array( 'numberposts' => -1, 'post_type'   => 'post',);
        $points = get_posts( $args );   
        $total = 0;

        $skill = array ('ecpt_editorial','ecpt_branding', 'ecpt_packaging');

        foreach( $points as $point ) {
            $single = get_post_meta( $point->ID, $skill, false );
            $total += $single;}

       echo $total;
       ++$total;
        ?>
1

There are 1 answers

6
David On

You cant pass in a array to the $key field. This is prob what your looking for:

    $args = array( 'numberposts' => -1, 'post_type'   => 'post',);
    $points = get_posts( $args );   
    $total = 0;

    $skill = array ('ecpt_editorial','ecpt_branding', 'ecpt_packaging');

    foreach( $points as $point ) {
        foreach($skill as $key){
             $single = get_post_meta( $point->ID, $key, false );
             var_dump($single);
             echo '<br>';
            ${$key} += (int) $single;
        }
   }

   foreach($skill as $key){
       echo $key.'='.${$key}; // there are now 3 variables with count values set, $ecpt_editorial , $ecpt_branding, $ecpt_packaging
   }

assuming your not looking to save $single for use?