Support

Account

Home Forums General Issues Count total amount of flexible content used?

Solving

Count total amount of flexible content used?

  • I’m trying to find a way to count how many times flexible content has been used in all of my posts.

    For example, I have four posts on my site. The first post has 4 flexible items in it, the second post has one, and third one has one as well. The last posts has zero flexible content. There should be a total of 6 flexible content items in all of the posts.

    When I use count to count the flexible content called ‘irl-today-data’ [ <?php $count = count(get_field(‘irl_today_data’));?>
    ], it shows up as 4 1 1. It’s suppose to be 6

    How do I make it to count all items total and not how many items each post has?

    My code:

    <?php $postcount = 0;
    $groups = [];
    foreach ( $wp_query->posts as $i => $p ) {
        $terms = wp_get_post_terms( $p->ID, 'show_category' );
        foreach ( $terms as $t ) {
            if ( ! isset( $groups[ $t->term_id ] ) ) {
                $groups[ $t->term_id ] = [];
            }
            $groups[ $t->term_id ][] =& $wp_query->posts[ $i ];
        }
    };?>
    
    <?php global $post; foreach ( $groups as $term_id => $posts ) { $term = get_term( $term_id );?>
                      
                      <?php foreach ( $posts as $post ) { setup_postdata( $post ); ?>
                        <?php if( have_rows('irl_today_data') ):?>
                            <?php $count = count(get_field('irl_today_data'));?>
                            <?php echo $count;?>  
                              <?php while ( have_rows('irl_today_data') ) : the_row();?>
                                <?php if( get_row_layout() == 'irl_today_website' ):?>
                                  
                                <?php endif;?>
                                <?php if( get_row_layout() == 'irl_today_social_media' ):?>
                                  
                                <?php endif;?>
                                
                              <?php endwhile;?>
                            <?php endif;?>
                            
                        <?php endif; ?>
                    <?php } } wp_reset_postdata();?>
  • When you are not on the page for the “Post” you need to include the post ID when calling ACF function, examples:

    
    have_rows('field name', $post_id);
    get_field('field name', $post_id);
    
  • Hi there,

    Here, you’re calculating a new count at each loop.
    By default a $variable will reset at each loop’s iteration.

    To calculate all iterations, you’ll need to concatenate your counter and you increment it at the end of each loop.

    Please, try something like this:

    <?php global $post; foreach ( $groups as $term_id => $posts ) { $term = get_term( $term_id );?>
                      
    	<?php foreach ( $posts as $post ) { setup_postdata( $post ); ?>
    	
    	<?php if( have_rows('irl_today_data') ):?>
    		  <?php $count = 0;
    		  while ( have_rows('irl_today_data') ) : the_row();
    		  $acf_loop_count .= $count; //Continue your count at each loop
    		  ?>
    		  
    			<?php if( get_row_layout() == 'irl_today_website' ):?>
    			  
    			<?php endif;?>
    			<?php if( get_row_layout() == 'irl_today_social_media' ):?>
    			  
    			<?php endif;?>
    			
    			<?php echo $acf_loop_count; //Echo inside the loop to output your counter at each loop (4 5 6) ?>
    			
    		  <?php $count++; endwhile;?>
    		  
    		  <?php echo $acf_loop_count; //Echo outside the loop to output your final value (6) ?>
    		  
    	<?php endif;?>
    		
    	<?php endif; ?>
    <?php } } wp_reset_postdata();?>

    Let me know if it helps,

    RemSEO

  • Thanks everyone for the help but it didn’t work so I re-did everything by adding another flexible content field inside another flexible content field like this:

    – IRL today data (flexible content)
    – Website (flexible content)
    – Image
    – Social (flexible content)
    – Text field

    Now I’m trying to find a way to count items inside an array. For example, when I run a print_r:

        <pre>
        <?php print_r(get_field('irl_today_entry', $post_id));?>
        </pre>

    It prints out:

    Array
    (
        [0] => Array
            (
                [acf_fc_layout] => irl_today_website_entry
                [irl_today_website] => Array
                    (
                        [0] => Array
                            (
                                [acf_fc_layout] => irl_today_img_website
                                [image_of_website] => http://soundboard.local/wp-content/uploads/2019/09/FireShot-Capture-003-Apple-Google-Pull-Hong-Kong-Protest-Apps-Amid-China-Uproar-WSJ_-www.wsj_.com_.png
                            )
    
                        [1] => Array
                            (
                                [acf_fc_layout] => irl_today_img_website
                                [image_of_website] => http://soundboard.local/wp-content/uploads/2019/09/FireShot-Capture-005-Apple-CEO-Tim-Cook-defends-decision-to-remove-an-app-used-by-Hong-Kon_-www.cnn_.com_.png
                            )
    
                    )
    
            )
    
        [1] => Array
            (
                [acf_fc_layout] => irl_today_social_entry
                [irl_today_social] => Array
                    (
                        [0] => Array
                            (
                                [acf_fc_layout] => irl_today_social_media
                                [social_media_network] => twitter
                                [social_media_username] => randouser
                                [text_of_social_media] => text removed
                                [url_to_social_media_post] => 
                                [social_media_date_published] => 
                            )
    
                        [1] => Array
                            (
                                [acf_fc_layout] => irl_today_social_media
                                [social_media_network] => twitter
                                [social_media_username] => randouser
                                [text_of_social_media] => text removed
                                [social_media_date_published] => 
                            )
    
                    )
    
            )
    
    )

    How do I count the items in [irl_today_social] without using any number?

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Count total amount of flexible content used?’ is closed to new replies.