Support

Account

Home Forums General Issues Total sum of custom field in post Reply To: Total sum of custom field in post

  • Hi,
    I’m doing this. I followed the steps from Elliot and works.

    This is my code:

    <?php 
      $total_beficio = 0; //my main variable
      $posts = get_posts(array(
        'posts_per_page'  => -1,      //get all post
        'post_type'     => 'apuestas' //my custom post type
      ));
    
        if( $posts ) {     
                
            foreach( $posts as $post ){ 
            
              setup_postdata( $post );
              if (get_field('beneficio')) {
                echo get_field('beneficio'); //show individual field(not needed)
                $total_beficio = get_field('beneficio') + $total_beficio;    //sum all fields 
              }
            }      
          
           wp_reset_postdata(); 
    
         }?>
    
    <?php echo '<h1>'.$total_beficio.'</h1>'//showing the total value;?>

    Well, I need a special function.

    I have a numbers fields what are summing.
    And I have a date field too. (Single date for every post, not start/end date)

    I need divide the field in months and years.

    Example:

    2017
    January
    30
    February
    23
    ….
    2016
    January
    23
    February
    73

    How I can achieve this?

    EDIT: I don’t need the wp published date. I need use my acf date field.

    Regards