Support

Account

Home Forums Add-ons Repeater Field How can I count all acf repeater fields belonging to the specific post type?

Helping

How can I count all acf repeater fields belonging to the specific post type?

  • I am currently trying to count all ACF repeater fields that belong to the determined post type.

    Post type: investors
    ACF Repeater Field: suchprofile

    Somehow I do not get any further. Or rather I get a value of 0.

    Is this because a loop is missing? I just want to count everything. For example, if a 20 investors (post type) each have 20 suchprofil (acf reapeater field) I want to output 400.

    function suchprofile_shortcode( $atts ) {
    	$rows = get_sub_field('suchprofil');
    	if( have_rows('suchprofil') );
    	$count = count($rows);
    	echo $count;
    } 
    add_shortcode( 'suchprofile', 'suchprofile_shortcode');

    Does anyone have an idea. Tried various posts and solutions.

    Best thanks
    Andy

  • In order to do this you would need to

    1. Do a WP_Query() to get all of the posts
    2. Loop over the posts
    3. use get_post_meta() on the repeater instead of get_field(), this will return the count of rows for the repeater rather than the field contents.
    4. Add the returned values.
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.