Support

Account

Home Forums Front-end Issues Find Sum of Custom Field Values from Different Posts

Solved

Find Sum of Custom Field Values from Different Posts

  • Ok so I have a custom post type called “Seasons”.

    These seasons are for example:
    2015/16
    2014/15
    2013/14

    In each of these posts, there is a custom field for “Goals”.

    How can I get the total goals from each season and then add them all together? i.e…

    2015/16 – 3 goals
    2014/15 – 10 goals
    2013/14 – 5 goals

    Total = 18 goals

    I know I can get the goals total manually for each season and then add them together, but I’d like to automatically add the total goals for all seasons (even when new seasons are added).

    I just can’t get my head around how to go about it. Any help would be hugely appreciated!

    Thanks

  • One of the answers on this page explains how to do this http://wordpress.stackexchange.com/questions/9394/getting-all-values-for-a-custom-field-key-cross-post, it’s the last one on the page right now posted by toscho

    The problem with the his however, as that poster says, there isn’t any way to not get values from revisions, trash, or all other types of posts.

    The only way to do this accurately it do use WP_Query to get all of the posts in your post type, loop through them to get all of the values and then add them together.

  • Not really getting anywhere with that unfortunately. Think I’ll just have to grab the values from each post manually 🙁

    Thanks for your help though!

  • You could reduce the number of posts returned and maybe speed it up a little by including a meta_query in your WP_Query to only return value > 0, that is if there ever are 0. No need to add up those.

  • Any chance of a code example explaining what I’d need to change? I’m a bit of a beginner with ACF!

  • What type of field is it? I’m assuming it’s a number field, I just want to make sure. Also what is the post type slug and the field name. You only gave me the labels above. Let me know and I’ll post some code for you.

  • Hi John,

    Thanks for your help with this so far. Really appreciate it.

    Here’s a screenshot showing exactly how the custom fields are currently set up: http://s29.postimg.org/xdinfl287/acf_screenshot.jpg

    So these custom fields are filled in for each season (post).

    There are 3 competition types, so in each season/post there would be for example:

    Competition 1
    Goals: 5

    Competition 2
    Goals: 2

    Competition 3
    Goals: 4

    What I basically want to do is get the total amount of goals for each competition from all seasons/posts. For example, I’d want the total goals across all seasons for Competition 1.

    I hope I’m not asking for too much here! Any help would be massively appreciated.

    Thanks,
    Steve

  • I think I’m still not sure what you want to display.

    Is this on the single season post and you are showing the total goals for each competition in that season?

    or

    Do you want to show the total for the competition across all seasons?

  • The latter.

    i.e…

    “Do you want to show the total for the competition across all seasons?”

    Is this doable?

    Thanks again.

  • If I understand what you’re looking for then something like this should do it.

    
    <?php 
      
      // not sure if you need this
      // if a template file then probably not
      global $post;
      
      // multidimensional array to hold stats
      $stats = array();
      
      $args = array(
        'post_type' => 'seasons', // not completely sure of post type
        'posts_per_page' => -1
      );
      $season_query = new WP_Query($args);
      if ($season_query->have_posts()) {
        if (have_rows('stats_bloc_m')) {
          while(have_rows('stats_blog_m')) {
            the_row();
            $competition = get_sub_field('competition_m');
            if (!isset($stats[$competition])) {
              $stats[$competition] = 0;
            }
            if (have_rows('competition_stats_m')) {
              while (have_rows('competition_stats_m')) {
                the_row();
                $stats[$competition] += get_sub_field('goals_m');
              } // end while have_rows('competition_stats_m')
            } // end if have_rows('competition_stats_m')
          } // end while have_rows('stats_blog_m')
        } // end if have_rows('stats_bloc_m')
      
      } // end if have posts
      
      if (count($stats)) {
        foreach ($stats as $competitions => $goals) {
          ?>
            <p><?php echo $competition; ?><br />Goals: <?php echo $goals; ?></p>
          <?php 
        } // end foreach stats
      } // end if count stats
      
    ?>
    
  • Sorry I left this so long but I’ve only just had chance to have a proper go with this code. It’s taken a lot of fiddling to tweak it to my needs but it’s all sorted now – John I can’t thank you enough for pointing me in the right direction!

    For others who need to know how to do this, my working code is below. This is a much simpler example where each post has a custom field of “rating”. We want to automatically get the rating from each post and add them all together to give a total rating.

    This goes inside your post loop:

    // Get Rating Totals
    if (have_rows('text_block')) {
        $i = 0;
        while(have_rows('text_block')) {
            the_row();
            $rating = get_sub_field('rating');
            $ratingsArray[$i++] += get_sub_field('rating');
        }
    }

    This outputs the total rating and should go outside of the loop:
    echo array_sum($ratingsArray);

    Cheers

Viewing 11 posts - 1 through 11 (of 11 total)

The topic ‘Find Sum of Custom Field Values from Different Posts’ is closed to new replies.