Skip to content

Instantly share code, notes, and snippets.

@josepedrodiaz
Last active October 10, 2023 19:52
Show Gist options
  • Select an option

  • Save josepedrodiaz/ed46c4119ff16b3c30889a24b7b7f9fb to your computer and use it in GitHub Desktop.

Select an option

Save josepedrodiaz/ed46c4119ff16b3c30889a24b7b7f9fb to your computer and use it in GitHub Desktop.

Revisions

  1. josepedrodiaz revised this gist Oct 10, 2023. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions flexible_modules.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    /*
    * Displays an array of used modules
    */
  2. josepedrodiaz created this gist Oct 10, 2023.
    47 changes: 47 additions & 0 deletions flexible_modules.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    /*
    * Displays an array of used modules
    */
    function display_used_modules() {
    //Name of the flexible content field
    $flexible_content_field_name = 'modules';

    echo 'Used modules <br />';

    /*pass your search string here example like this ( 's'=>'test' ) */
    $args=array(
    'posts_per_page' => -1,
    'post_type' => 'page',
    );

    $query=new WP_Query($args);

    if( $query->have_posts()):

    $layouts = array();

    while( $query->have_posts()): $query->the_post();

    {
    $post_id = get_the_ID();
    $modules = get_field($flexible_content_field_name, $post_id);
    if(is_array($modules)){
    $module_layouts = array_column($modules, 'acf_fc_layout');
    foreach ($module_layouts as $layout) {
    if (!in_array($layout, $layouts)) {
    $layouts[] = $layout;
    }
    }
    }
    }

    endwhile;

    print_r($layouts);

    echo 'Total posts: ' . $query->found_posts;

    endif;

    die;
    }
    add_action('init', 'display_used_modules', 10, 0);