Last active
October 10, 2023 19:52
-
-
Save josepedrodiaz/ed46c4119ff16b3c30889a24b7b7f9fb to your computer and use it in GitHub Desktop.
Get flexible modules in use
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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