Home › Forums › Add-ons › Flexible Content Field › Access Nested Flexible Content using get_post_meta
Hi All,
I have a nested flexible content which has a parent flexible layout called section_layout where you can add columns. Inside each column I have another flexible layout called elements which contains 4 layouts like title, content and so on.
I am trying to access the flexible content using get_post_meta and having an issue accessing the nested flexible content.
Below is my code I have so far.
FIRST ACF CODE using while(), have_rows(), get_row_layout() and get_sub_field() .
<?php
$post_id = get_the_ID();
if ( have_rows( 'section_layout', $post_id ) ):
while ( have_rows( 'section_layout', $post_id ) ) : the_row();
if ( get_row_layout() == 'single_column', $post_id ) :
if ( have_rows( 'single_col_elements_content_type', $post_id ) ):
while ( have_rows( 'single_col_elements_content_type', $post_id ) ) : the_row();
// get each layout from the nested flexible content
get_template_part( 'partials/flexible-layouts/' . get_row_layout() );
endwhile;
else:
// no layouts found
endif;
elseif ( get_row_layout() == 'two_columns', $post_id ) :
if ( have_rows( 'col1_elements_content_type', $post_id ) ):
while ( have_rows( 'col1_elements_content_type', $post_id ) ) : the_row();
// get each layout from the nested flexible content
get_template_part( 'partials/flexible-layouts/' . get_row_layout() );
endwhile;
else:
// no layouts found
endif;
if ( have_rows( 'col2_elements_content_type', $post_id ) ):
while ( have_rows( 'col2_elements_content_type', $post_id ) ) : the_row();
// get each layout from the nested flexible content
get_template_part( 'partials/flexible-layouts/' . get_row_layout() );
endwhile;
else:
// no layouts found
endif;
elseif ( get_row_layout() == 'three_columns', $post_id ) :
if ( have_rows( 'col1_elements_content_type', $post_id ) ):
while ( have_rows( 'col1_elements_content_type', $post_id ) ) : the_row();
// get each layout from the nested flexible content
get_template_part( 'partials/flexible-layouts/' . get_row_layout() );
endwhile;
else:
// no layouts found
endif;
if ( have_rows( 'col2_elements_content_type', $post_id ) ):
while ( have_rows( 'col2_elements_content_type', $post_id ) ) : the_row();
// get each layout from the nested flexible content
get_template_part( 'partials/flexible-layouts/' . get_row_layout() );
endwhile;
else:
// no layouts found
endif;
if ( have_rows( 'col3_elements_content_type', $post_id ) ):
while ( have_rows( 'col3_elements_content_type', $post_id ) ) : the_row();
// get each layout from the nested flexible content
get_template_part( 'partials/flexible-layouts/' . get_row_layout() );
endwhile;
else:
// no layouts found
endif;
endif;
endwhile;
else:
// no layouts found
endif;
?>
SECOND get_post_meta using foreach(), switch() and get_post_meta()
$flex_modules = get_post_meta( $post_id, 'section_layout', true );
foreach( $flex_modules as $count => $flex_module ) {
switch( $flex_module ) {
case 'single_column':
$column_module = get_post_meta( $post_id, 'section_layout_single_column'.$count.'_single_col_elements_content_type', true );
break;
}
}
The problem the switch never kicks in even though I can see the array of $flex_modules.
What am I missing? or am I better of sticking to using ACF options instead of get_post_meta
Thanks and appreciate it
Tammu
The layout name is not part of the meta key, I’m not sure about your code but it might be
$column_module = get_post_meta( $post_id, 'section_layout_'.$count.'_single_col_elements_content_type', true );
A nested flex field, or fields would be something like
"{$flex_field_name}_{$index}_{$nested_flex_name}_{$index}_{$sub_field_name}"
Hi John,
Thank you for replying, I am able to get all the nested content with the help of your reply using get_post_meta.
I do want to ask you, Am I better of using built-in ACF fields or use get_post_meta in regards to performance in the frontend.
Thanks and appreciate it
Tammu
There should be very little difference between get_field() and get_post_meta() for performance in most cases. There are some exceptions to this. It depends on the field and what it is set to return, for example gallery fields that return arrays of image data. ACF function are, for the most part, wrappers for WP functions.
The main difference with using get_post_meta() is that you can deactivate ACF in the admin and the front end of your site will continue to function until you reactivate.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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.