Support

Account

Home Forums Add-ons Flexible Content Field See how many times each layout is used in all pages

Solving

See how many times each layout is used in all pages

  • Hi,

    Is there a way to loop through all pages and count how many times each layout is used in a flexible conent field?

    For some context we have a ‘Page Builder’ with a whole bunch of layouts (around 66). I wanted to see how many times in total each layout is used throughout all the pages, there’s hundreds of pages and no easy way to get these numbers so was hoping there was a function out there.

    Thanks,
    Benji

  • I would not try to do a loop over that many posts and fields, more then likely it would simply time out. I would do a query directly on the db to get what I want.

    
    global $wpdb;
    /* 
      the following does not use $wpdb->prepare()
      when I'm doing queries like this I am in control of all values
      and I make sure that everything is correct
    
      also, there is not "JOIN" because there is no difference in performance
      between my way to join tables and the """Right Way"""
    */
    global $wpdb;
    $query = 'SELECT meta_value 
              FROM '.$wpdb->postmeta.' pm, '.$wpdb->posts.' p
              WHERE pm.meta_key = "{FLEX FIELD NAME HERE}"
                AND pm.post_id = p.ID
                AND p.post_status = "publish"';
    $values = $wpdb->query($query);
    $counts = array();
    if ($values) {
      foreach ($values as $value) {
        $value = maybe_unserialize($value['meta_value']);
        if (!empty($value)) {
          foreach ($value as $layout) {
            if (!isset($counts[$layout])) {
              $counts[$layout] = 0;
            }
            $counts[$layout]++;
          }
        }
      }
    }
    echo '<pre>'; print_r($counts); echo '</pre>';
    
    • AdamACF

    • March 31, 2021 at 10:58 am

    This works well if you aren’t using repeaters/flexible content fields etc. If you are however and you want to search by an inner field then this won’t return any results.

    • AdamACF

    • March 31, 2021 at 11:11 am

    I had admittedly slightly misunderstood the request of this post, there is clearly a different between field layouts and specific fields.

    Still, if you do want to find subfields of a field in a field group try..

    SELECT *
              FROM wpas_postmeta pm, wpas_posts p
              WHERE pm.meta_value LIKE "%paragraph_with_large_image%"
                AND pm.post_id = p.ID
                AND p.post_status = "publish"
Viewing 4 posts - 1 through 4 (of 4 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.