Skip Navigation

[Resolved] Count number of entries in repeating field, doesn’t work if there are no entries

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a custom field that allows multiple values. I need to be able to count how many items exist in that custom field in each post, so I created a custom shortcode. However, it doesn't seem to work accurately when no items exist. It returns 1 instead of 0.

Solution: In a basic case the following code will work, but if you leave the first instance blank and add content in the following instances, this code will not work. You'll need to add additional logic to handle those cases.

add_shortcode('count_videos', 'count_videos_func');
function count_videos_func($atts, $content){
    $atts = shortcode_atts( array(
        'field' => '',
        'post_id' => get_the_ID(),
    ), $atts );
 
    $field = get_post_meta($atts['post_id'], $atts['field'], false);
    $res = 0;
    if(is_array($field) && $field[0] != ''){
        $res = count($field);
    }
    return $res;
}
This support ticket is created 5 years, 5 months 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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by ericE-4 5 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1309015

I am trying to use a shortcode for a custom function from https://toolset.com/forums/topic/count-number-of-entries-in-repeating-field/#post-1207355 so I can include the count of items in a repeating field in my content template.

The field in question is an Embedded Media field, set to allow multiple instances.

I have included the following code in my functions.php:

add_shortcode('count_videos', 'count_videos_func');
function count_videos_func($atts, $content){
    $atts = shortcode_atts( array(
        'field' => '',
        'post_id' => get_the_ID(),
    ), $atts );
      
    $field = get_post_meta($atts['post_id'], $atts['field'], false);
    $res = 0;
    if(is_array($field)){
        $res = count($field);
    }
    return $res;
}

In my content template I have the shortcode like this:

[count_videos field='wpcf-videos']

If there are 3 videos in the field for a post, "3" is displayed. However if there are no videos in the field, "1" is displayed.

How can I make it accurately display "0" if there are no videos?

#1309019

In a very basic case, you can use the following code:

add_shortcode('count_videos', 'count_videos_func');
function count_videos_func($atts, $content){
    $atts = shortcode_atts( array(
        'field' => '',
        'post_id' => get_the_ID(),
    ), $atts );

    $field = get_post_meta($atts['post_id'], $atts['field'], false);
    $res = 0;
    if(is_array($field) && $field[0] != ''){
        $res = count($field);
    }
    return $res;
}

The problem here is if you create 4 instances of this repeating field and store nothing in instance 1, but store something in instances 2, 3, and 4, this code will not work correctly. If the first instance is empty, the code assumes no other instances exist. You would have to add some additional custom logic to handle the case where it's possible to save a blank value in multiple, but not all, instances.

#1309029

Thanks. I wonder if the poor guy who was given the first code realizes it doesn't work...

This one works for me--I won't store any field instance that doesn't have data.