Support

Account

Home Forums Add-ons Flexible Content Field Count flexible content rows of a particular row layout

Helping

Count flexible content rows of a particular row layout

  • I found the following snippet on another thread. This counts all the rows in a flexible content field.

    $field_object = get_field_object(‘flexible_content_field’);
    $total_rows = count($field_object[‘value’]);

    I’m wondering if its possible to refine the count so that only rows that have a particular layout are counted.

  • The postmeta value stored for a flexible content field is an array where each element of the array is the layout of that row. I would do something like this.

    
    // get the post meta value without using ACF
    $rows = get_post_meta($post_id, 'flexible_content_field', true);
    // initialize layout counts in case there is nothing in the meta
    $layout_counts = array();
    if (is_array($rows)) {
      // have php count the values of the array
      $layout_counts = array_count_values($rows);
    }
    // initialize count at 0
    $count = 0;
    // see if any count for a layout
    if (isset($layout_counts['layout_1'])) {
      $count = $layout_counts['layout_1'];
    }
    echo $count;
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.