Skip to content

Instantly share code, notes, and snippets.

@josepedrodiaz
Last active October 10, 2023 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josepedrodiaz/ed46c4119ff16b3c30889a24b7b7f9fb to your computer and use it in GitHub Desktop.
Save josepedrodiaz/ed46c4119ff16b3c30889a24b7b7f9fb to your computer and use it in GitHub Desktop.
Get flexible modules in use
<?php
/*
* 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment