ShortCode to count amount of posts having certain Custom Field values as passed in a ShortCode attribute.

add_shortcode( 'count-siege-by-shortcode-attr', 'count_siege_by_attr' );

function count_siege_by_attr( $atts, $content = '' ) {
    
    //Hier bilden wir ein attribut für den shortcode, wo alle werte des sieges eingegeben werden, komma separiert.
    // beispiel: [count-siege-by-shortcode-attr type="Championsleague,Landesleague, Nationalcup, etc"]
    $a = shortcode_atts( [
                'type'   => false,
    ], $atts );

    //die ausgbe des ShortCodes ist erst mal leer
    $output = '';

    //falls wir ein Pokal Typ (type attribute) angeben, dann extrahieren wir dies und bilden ein array
    if ( $a['type'] ) {
        // Parse type into an array. Whitespace will be stripped.
        $a['type'] = array_map( 'trim', str_getcsv( $a['type'], ',' ) );
    }

    //nun holen wir uns alle posts von der datenbasis, post typ "siege", wo das feld text-veranstaltung eines der werte von Sieges Typ hat (type attribute). Wir tun dies mit WP_Query und shieben den array von sieges typen rein, um bloss die Posts zu holen welche wir wollen!
    $args = array(
	    'post_type' => 'post',
	    'meta_query' => array(
	       
	        array(
			    'key' => 'wpcf-our-field',//das ist das feld auf das wirt hören wollen
			    'value' => $a['type'],//$a['type'] haben wir weiter oben im attribut definiert!
			    'compare' => 'IN'
			),
	       //hier müssen etliche NEUE werte als neuer array (also so wie die obigen 2 beispiele) engefügt werden. Wir können damit immer helfen fals nötig
	    )
	);
	$query = new WP_Query( $args );

	//wir zählen alle posts die wir bekamen
    $total=count($query->posts);

    //und nun spucken wir die nummer in einer kette (gezählt) aus.
    $output .= $total;

    //Shortcode machen niemals ECHO, die machen immer RETURN
    return $output;
}

Use the ShortCode like this:
[count-siege-by-shortcode-attr type="Landespokal,Supercup,ETC"]

Get and Post repeating Fields correctly, persisting the order

This code sample shows how to get and post repeating Fields in Toolset (created with Types)

Get the Fields and their order:

//We are getting a Repeating Field (MultiLine) from the Database.
//This will give us an Array() of Repeating Instances. 
$repeating_field = get_post_meta($post_id, 'wpcf-{your-field-slug}');
 
//We also need the hidden field for the above repeating Field, to determine the order wich is used for each instance (_wpcf-{your-field-slug}-sort-order)
$sort_order_repeating = get_post_meta($post_id, '_wpcf-{your-field-slug}-sort-order', true);

Update/Create Fields and their Order:

//go over all repeating fields instances
foreach ($repeating_field as $repeating_field_single_instance ) {
 
  //create a new $variable with single instance
  $single_value = $repeating_field_single_instance;
 
  //add the repeating possible answers
  add_post_meta($new_post_id, 'wpcf-{your-field-slug-new}', $single_value);
}
 
//update the order of those repeating fields
update_post_meta($new_post_id, '_wpcf-{your-field-slug-new}-sort-order', $sort_order_repeating);

This is a custom code snippet that needs to be adapted. It does NOT use public documented API, and it might change in future. We do NOT help on further custom code with this kind of data, but we show how we can get/post it

Check Views results

To check if a View returns any results at all or count the results (amount of posts) of the View, you can use the function get_view_query_results()

An example

$filtered_posts = get_view_query_results( 21 );//change to ID of first View 
$all_out = count($filtered_posts);

Example Threads:

https://wp-types.com/forums/topic/conditional-output-with-found-count-or-items-count

https://toolset.com/forums/topic/view-filter-and-sorting/

Count Repeating Field / Count multiple instances

We can achieve this by using custom shortcode. Please add this code in your theme’s or child theme’s functions.php file:


add_shortcode( 'count-repeats', 'count_repeat_func' );
function count_repeat_func($atts) {
return sizeof(get_post_meta( $atts['post-id'], 'wpcf-' . $atts['field'], false ));
}

Then you can use this ShortCode. You should wrap this ShortCode in an HTML Conditional to check if the Field does exist, as other wise unexpected results can be produced:

[count-repeats post-id="6184" field="url"]

More details: https://wp-types.com/forums/topic/how-to-count-repeated-field/#post-412707

Count total of repeating Fields on single Post

This ShortCode lets you print a numerical total of the repeating Fields on a Single Post.

You need to pass the Custom Field Slug to the ShortCode (without the wpcf- prefix)

Function:

// shortcode to count number of repeating field instances
add_shortcode( 'count-repeats', 'count_repeat_func' );
function count_repeat_func($atts) {
    return sizeof(get_post_meta( get_the_ID(), 'wpcf-' . $atts['field'], false ));
}

ShortCode Example:

[count-repeats field="your-field-slug"]

Calculate sum of Custom Fields in a View, as in += PHP operator.

Register this 2 ShortCodes:

global $total;
function add_total_shortcode($atts, $content = '') {
global $total;
 
$total += wpv_do_shortcode($content);
}
 
add_shortcode('add-to-total', 'add_total_shortcode');
function show_total_shortcode() {
global $total;
$totalNew = $total;
$total = 0;
return $totalNew;
}
 
add_shortcode('show-total', 'show_total_shortcode');</code>

Register those ShortCodes in Views > Settings > Compatibility > 3rd Party ShortCodes

Use the ShortCodes in a View as this:

<wpv-loop>
    
    [add-to-total]
      
    [/add-to-total]
</wpv-loop>
[show-total]