Home › Forums › Add-ons › Flexible Content Field › Content after custom loop inside flexible content
Hi all!
I’m facing some issues to display content inside a custom post type after a custom WP loop (WP_Query) inside ACF flexible content.
To display content before the WP_Query works fine. But not after.
wp_reset_postdata() is implemented after the end of the custom loop. But without any luck.
The custom post type handle the index content for the page.
And ACF Flexible Content handles the content inside each page. This to be able to have multiple “sections” on one page.
Se the code below.
Shorted down and cleaned from all the HTML. Since it’s not essential in the problem.
<?php
get_header();
$args = array(
'post_type' => 'sm_indexsections',
'posts_per_page' => -1
);
$query = new WP_Query($args);
// The custom post type loop
if($query->have_posts()):
while ($query->have_posts()):
$query->the_post();
// ACF Flexible content loop
if(have_rows('acfIndCont')) {
while(have_rows('acfIndCont')) {
the_row();
switch(get_row_layout()) {
// Show blog-posts where a certain criteria is met
case 'acfIndSecBlogPosts':
$postsArgs = array(
'posts_per_page' => 5,
'post_type' => 'post',
'meta_key' => 'acfKnowHubPostCaseShow',
'meta_value' => true
);
$postsQuery = new WP_Query($postsArgs);
if($postsQuery->have_posts()) {
while($postsQuery->have_posts()) {
$postsQuery->the_post();
// Display all blog-posts where this criteria is met
}
}
// Tried reset_query aswell but wit no success.
// wp_reset_query();
wp_reset_postdata();
break;
// To put this (or other flexible content) before the custom loop works. But not after.
// there is no loop inside this case.
case 'acfIndSecBtn':
// Other content to display
break;
}
}
}
endwhile;
endif;
wp_reset_postdata();
get_footer();
?>
Can anybody see what’s wrong with this? With my knowledge this should work. But it doesn’t.
Thanks!
Try moving your wp_reset_postdata()
calls inside your if($query->have_posts())
and if($postsQuery->have_posts())
statements.
https://developer.wordpress.org/reference/functions/wp_reset_postdata/#comment-2325
Thanks for your answer.
Hmm. Nope. didn’t work.
Forgot to mention that i did try to move the call inside if($postsQuery->have_posts())
earlier.
The problem here is that you have multiple nested queries. This comes up often. When you have multiple nested queries you cannot loop over the posts in the 3rd, 4th, etc, queries using the standard WP loop
while ($query->have_post()) {
.... etc
The reason for this is that wp_reset_postdata()
always resets the query and the post data to the main WP query and not the previous WP query.
see my notes and changes
<?php
get_header();
// main WP query happens before here
// this is the 1st nested query
$args = array(
'post_type' => 'sm_indexsections',
'posts_per_page' => -1
);
$query = new WP_Query($args);
// The custom post type loop
if($query->have_posts()):
while ($query->have_posts()):
$query->the_post();
// ACF Flexible content loop
if(have_rows('acfIndCont')) {
while(have_rows('acfIndCont')) {
the_row();
switch(get_row_layout()) {
// Show blog-posts where a certain criteria is met
case 'acfIndSecBlogPosts':
// This is the second nested query
$postsArgs = array(
'posts_per_page' => 5,
'post_type' => 'post',
'meta_key' => 'acfKnowHubPostCaseShow',
'meta_value' => true
);
$postsQuery = new WP_Query($postsArgs);
// you must access the posts in this query useing other methods
/* NOT THIS
if($postsQuery->have_posts()) {
while($postsQuery->have_posts()) {
$postsQuery->the_post();
// Display all blog-posts where this criteria is met
}
}
END NOT THIS */
// USE SOMETHING LIKE THIS
// also do not use the global $post varaible
if (count($postsQuery->posts)) {
foreach ($postsQuery->posts as $nested_post) {
// and exampele
echo get_the_title($nested_post->ID);
}
}
// DO NOT USE EITHER OF THESE FUNCTIONS
// IN 2ND AND AFTER NESTED QUERIES
// wp_reset_query();
//wp_reset_postdata();
break;
// To put this (or other flexible content) before the custom loop works.
//But not after.
// there is no loop inside this case.
case 'acfIndSecBtn':
// Other content to display
break;
}
}
}
endwhile;
endif;
wp_reset_postdata();
get_footer();
?>
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!
🤔 Curious about the ACF user experience? So are we! Help guide the evolution of ACF by taking part in our first ever Annual Survey and guarantee you’re represented in the results. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 8, 2023
© 2023 Advanced Custom Fields.
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.