I have a page with a few ACF fields (image and text in a group). That works fine when used alone.
Then I’ve added some get_posts code to pull in posts across a multisite network. That code functions perfectly well.
Problem is, no ACF fields work after that code runs. Thought it was a problem with switch to blog, but no matter what variations I try, I can’t get ACF to find the current blog again. Latest version:
<?php switch_to_blog(2); $tallimage2 = get_field('tall_image_2'); if( $tallimage2 ): ?> <img src="<?php echo $tallimage2['image2']; ?>"> <p class="mb0"><?php echo $tallimage2['caption2']; ?></p> <?php endif; ?> <?php restore_current_blog(); ?>
Appreciate any advice!
I’m not sure how this is working correctly at all by looking at the code you’ve provided
<?php switch_to_blog(2); // ACF will be using the current post ID here // that current post ID is the post on the current site which may // be a different post with the same ID on the site you are switching to // I can only guess that this is somehow causing the post ID to get lost $tallimage2 = get_field('tall_image_2'); if( $tallimage2 ): ?> <img src="<?php echo $tallimage2['image2']; ?>"> <p class="mb0"><?php echo $tallimage2['caption2']; ?></p> <?php endif; ?> <?php restore_current_blog(); ?>
I would start by seeing what the post is before and after your call by outputting the global $post value before and after
global $post; echo '<pre>'; print_r($post); echo '</pre>';
Hi John, if by “before and after” you mean that I should use this code before the first switch to blog and then after the last one, then that’s what I’ve done here:
The first ID is 256 which is the page linked above. The ID 277 is from the Coffee Job Berlin post which is the last post being pulled in on the right hand side of the page. So it would appear that my code for getting posts from three different subdomains (thats the coworking, consulting and coffee job berlin links on the right hand side) is not fully resetting back to the current site?
Confused by your note “ACF will be using the current post ID here”. Everything I read said that switchtoblog is for getting the SITE ID, not post ID. And that restoreblog is for switching back to the SITE ID where the current page is.
Very much appreciate your insights!
Found a super simple fix… since John thought the POST ID was getting lost with all of the switch to blogs, it occurred to me to simply add the post ID via this method:
The topic ‘switch_to_blog problem’ is closed to new replies.
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!