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