Skip Navigation

[Resolved] Sum filesize of all images attached to a certain post type

This support ticket is created 4 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 5.52 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Tagged: 

This topic contains 16 replies, has 3 voices.

Last updated by Lara 4 years ago.

Assisted by: Waqar.

Author
Posts
#1815999

Thanks for the update and glad that it is working now.

You're welcome to share the final code snippet in this thread and it will be very useful for other users, with a similar requirement.

#1827625

Many apologies for the delay, I have been sick. Here are the final code snippets:

1. Custom code snippet
-----> Go to Toolset Settings --> Custom Code --> Create New Snipped and enter below code (either VARIATION A or VARIATION B)

VARIANTION A:

Outputs all posts of the post type 'rask' where the current user is the post author and the custom field 'wpcf-rask-welches-journal-termine' has the value '6'. Please remove 'rask' in the code below and insert the post slug of your own post instead.
If you remove the following line ...

 'author'   => get_current_user_id(), 

... in the code below, then it will output all posts - and not only those of the current user.
If you don't need to filter a custom field, then please remove the following lines..

    'meta_query' => array(
        array(
            'key'   => 'wpcf-rask-welches-journal-termine',
            'value' => '6',
        )
    )

of the code below. If you need to filter a different custom field, please remove 'wpcf-rask-welches-journal-termine' and insert 'wpcf-your-custom-field-slug' instead. Remove '6' and insert the value of your custom field.

IMPORTANT: You need to use your own custom fields slugs for Custom Field A (wpcf-rask-bilder --> into: wpcf-your-custom-field-slug-a) and Custom Field B (wpcf-rask-video --> into: wpcf-your-custom-field-slug-b). You can also remove Custom Field B or add an additional Custom Filed C. Please change the code accordingly.

The code:


add_shortcode( 'sum_files', 'calc_func' );

function calc_func( $atts ){
   
// get all Posts of your type
        $all_posts = get_posts(array(
            'numberposts'   => -1,
            'post_type'     => 'rask',
            'author'   => get_current_user_id(),
    'meta_query' => array(
        array(
            'key'   => 'wpcf-rask-welches-journal-termine',
            'value' => '6',
        )
    )


            )
        );
  
  

   
//if it returns some posts
        if( $all_posts ){
  
//Start the count on 0
$single_posts_value_sum = 0;
$single_posts_value_sum_a = 0;
$single_posts_value_sum_b = 0;

  
            //now get the single posts fields values 
            foreach( $all_posts as $single_post ){ 
   
                //get each Posts post data
                $single_post_data = get_post($single_post);
                //get each ID
                $single_post_id = $single_post_data->ID;
                                //get each posts field value
              

              

              
//Custom Field A      
  
                
$value_images_array_a = get_post_meta($single_post_id,'wpcf-rask-bilder', false);
                         
              
              
//check if 'file_url_a' is set?
if(!empty($value_images_array_a) ) {                  
  
foreach($value_images_array_a as $value_images_a){
  
//Get the header response for the file in question.
$headers_a = get_headers($value_images_a, 1);

//Convert the array keys to lower case for the sake
//of consistency.
$headers_a = array_change_key_case($headers_a);

//Set to -1 by default.
$filesize_a = -1;

//Check to see if the content-length key actually exists in
//the array before attempting to access it.
if(isset($headers_a['content-length'])){
$filesize_a = $headers_a['content-length'];
  

//Sum the values all posts fields  
$single_posts_value_sum_a+= $filesize_a; 


}
                              
}
}
                       

            
              
//Custom Field B      

              
$value_images_array_b = get_post_meta($single_post_id,'wpcf-rask-video', false);
              
$image_filesize_sum_b = 0;  
              
//check if 'file_url_b' is set?
if(!empty($value_images_array_b) ) {           
  
foreach($value_images_array_b as $value_images_b){
    
  
//Get the header response for the file in question.
$headers_b = get_headers($value_images_b, 1);

//Convert the array keys to lower case for the sake
//of consistency.
$headers_b = array_change_key_case($headers_b);

//Set to -1 by default.
$filesize_b = -1;

//Check to see if the content-length key actually exists in
//the array before attempting to access it.
if(isset($headers_b['content-length'])){
  
$filesize_b = $headers_b['content-length'];
    
  
//Sum the values all posts fields  
$single_posts_value_sum_b+= $filesize_b; 


}                              
}
}
                         
              
//end of Custom Field B, close. If you don't need a Custom field B, please remove the entire code and also remove  + $single_posts_value_sum_b in the line below.
           
}               
}
  
$single_posts_value_sum = $single_posts_value_sum_a + $single_posts_value_sum_b;   
  
//Convert it into KB
$single_posts_value_sum_KB = round($single_posts_value_sum / 1024);
$single_posts_value_sum_a_KB = round($single_posts_value_sum_a / 1024);
$single_posts_value_sum_b_KB = round($single_posts_value_sum_b / 1024);
  
return $single_posts_value_sum_KB . ' KB ( Bilder: ' . $single_posts_value_sum_a_KB . ' KB | Videos: ' . $single_posts_value_sum_b_KB . ' KB ) ';
  
}
   

If you only need the amount of KB, then change the last lines of the code above into:

return $single_posts_value_sum_KB;

VARIANTION B:

The code below takes the post id of the current parent post and outputs all its childposts. Please replace 'pferd-rask' through your own relationship slug.

IMPORTANT: You need to use your own custom fields slugs for Custom Field A (wpcf-rask-bilder --> into: wpcf-your-custom-field-slug-a) and Custom Field B (wpcf-rask-video --> into: wpcf-your-custom-field-slug-b). You can also remove Custom Field B or add an additional Custom Filed C. Please change the code accordingly.

add_shortcode( 'sum_files', 'calc_func_rask_sum' );

function calc_func_rask_sum( $atts ){

extract( shortcode_atts( array('id' => '',), $atts ) );
  
//URL of the remote file that you want to get
//the file size of. 
$id = $atts['id'];
   
$child_posts = toolset_get_related_posts($id,'pferd-rask','parent',999,0,array(),'post_id','child');
  


   
//if it returns some posts
        if( $child_posts){
  
//Start the count on 0
$single_posts_value_sum = 0;
$single_posts_value_sum_a = 0;
$single_posts_value_sum_b = 0;

  
            //now get the single posts fields values 
            foreach( $child_posts as $single_post ){ 
   
                //get each Posts post data
                $single_post_data = get_post($single_post);
                //get each ID
                $single_post_id = $single_post_data->ID;
                                //get each posts field value
              

              

              
//Custom Field A      
  
                
$value_images_array_a = get_post_meta($single_post_id,'wpcf-rask-bilder', false);
                         
              
              
//check if 'file_url_a' is set?
if(!empty($value_images_array_a) ) {                  
  
foreach($value_images_array_a as $value_images_a){
  
//Get the header response for the file in question.
$headers_a = get_headers($value_images_a, 1);

//Convert the array keys to lower case for the sake
//of consistency.
$headers_a = array_change_key_case($headers_a);

//Set to -1 by default.
$filesize_a = -1;

//Check to see if the content-length key actually exists in
//the array before attempting to access it.
if(isset($headers_a['content-length'])){
$filesize_a = $headers_a['content-length'];
  

//Sum the values all posts fields  
$single_posts_value_sum_a+= $filesize_a; 


}
                              
}
}
                       

          

              
              
              
              
//Custom Field B      

              
$value_images_array_b = get_post_meta($single_post_id,'wpcf-rask-video', false);
              
$image_filesize_sum_b = 0;  
              
//check if 'file_url_b' is set?
if(!empty($value_images_array_b) ) {           
  
foreach($value_images_array_b as $value_images_b){
    
  
//Get the header response for the file in question.
$headers_b = get_headers($value_images_b, 1);

//Convert the array keys to lower case for the sake
//of consistency.
$headers_b = array_change_key_case($headers_b);

//Set to -1 by default.
$filesize_b = -1;

//Check to see if the content-length key actually exists in
//the array before attempting to access it.
if(isset($headers_b['content-length'])){
  
$filesize_b = $headers_b['content-length'];
    
  
//Sum the values all posts fields  
$single_posts_value_sum_b+= $filesize_b; 


}                              
}
}
                         
              
//end of Custom Field B, close. If you don't need a Custom field B, please remove the entire code and also remove  + $single_posts_value_sum_b in the line below.
           
}               
}
  
$single_posts_value_sum = $single_posts_value_sum_a + $single_posts_value_sum_b;   
  
//Convert it into KB
$single_posts_value_sum_KB = round($single_posts_value_sum / 1024);
$single_posts_value_sum_a_KB = round($single_posts_value_sum_a / 1024);
$single_posts_value_sum_b_KB = round($single_posts_value_sum_b / 1024);
  
return 'Du hast bereits ' . count($child_posts) . ' Einträge zeitlich unbegrenzt gespeichert und ' .$single_posts_value_sum_KB . ' KB ( Bilder: ' . $single_posts_value_sum_a_KB . ' KB | Videos: ' . $single_posts_value_sum_b_KB . ' KB ) ';
  
}

count($child_posts) --> returns how many child posts for this parent exists.

If you only need the amount of KB, then change the last lines of the code above into:

return $single_posts_value_sum_KB;

2. Register Shortcode "sum_files"
-----> Go to Toolset Settings --> Front-end Content ---> Third-party shortcode arguments --> Enter sum_files in the field "Shortcode name" and click add